android - EditText not working in RecyclerView Adapter. Random Data is displayed on device -
im making app takes movie name input user, after input new screen displayed showing list items of movie, list of movie shown "http://www.omdbapi.com/" imdb. on clicking list item(movie name) details of movie shown in screen, working fine before getting input, hard code movie name before want user give inout think there's problem in edit text i'm unable input user.
mainactivity class
public class mainactivity extends appcompatactivity { private static string log_tag = mainactivity.class.getsimplename(); private edittext edittext; private button button; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); edittext = (edittext) findviewbyid(r.id.edittext); button = (button) findviewbyid(r.id.button1); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { validateinput(); } }); } private void validateinput() { string input = edittext.gettext().tostring(); if (input != null || !input.equals("")) { intent intent = new intent(this, moviefragment.class); intent.putextra(intent.extra_text, input); startactivity(intent); } setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab); fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { snackbar.make(view, "replace own action", snackbar.length_long) .setaction("action", null).show(); } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); }
moviefragment class
public class moviefragment extends fragment implements onnetworkcallhandled, recyclerviewadapter.onclicklistener { private string moviename; private button button; private recyclerviewadapter adapter; private recyclerview recyclerview; private edittext edittext; private datawrapper list; public moviefragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { intent intent = getactivity().getintent(); view rootview = inflater.inflate(r.layout.fragment_main, container, false); recyclerview = (recyclerview) rootview.findviewbyid(r.id.recyclerview); if (intent != null && intent.hasextra(intent.extra_text)) { moviename = intent.getstringextra(intent.extra_text); ((textview) rootview.findviewbyid(r.id.detail_text)).settext(moviename); } getmoviedetails(); return rootview; } private void getmoviedetails() { movietask movieasynctask = new movietask(this, moviename); movieasynctask.execute(); } @override public void onnetworkcallsuccess(object object) { system.out.println("on success"); string result = (string) object; gson gson = new gson(); list = gson.fromjson(result, datawrapper.class); adapter = new recyclerviewadapter(list.getsearch(), getcontext(), this); recyclerview.setlayoutmanager(new linearlayoutmanager(getcontext())); recyclerview.setadapter(adapter); system.out.println("on success"); } @override public void onnetworkcallfailure(object object) { system.out.println("on failure"); } @override public void onclick(view view, int position, long itemid) { startactivity(position); system.out.println(); } private void startactivity(int position) { intent intent = new intent(getactivity(), detailactivity.class); bundle extras = new bundle(); extras.putstring("title",list.getsearch().get(position).gettitle()); extras.putstring("year", list.getsearch().get(position).getyear()); intent.putextras(extras); startactivity(intent); }
}
detailactivity class
public class detailactivity extends appcompatactivity { private textview title; private textview year; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_detail); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); getsupportactionbar().setdisplayhomeasupenabled(true); intent intent = getintent(); bundle extras = intent.getextras(); title = (textview) findviewbyid(r.id.title); year = (textview) findviewbyid(r.id.year); if (intent != null) { string title = extras.getstring("title"); string year=extras.getstring("year"); title.settext(title); year.settext(year); system.out.println(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; }
}
movietask extending asynctask class
public class movietask extends asynctask<void, void, string> { private string log_tag = movietask.class.getsimplename(); private string response = ""; private string moviename; private onnetworkcallhandled onnetworkcallhandled; public movietask(onnetworkcallhandled onnetworkcallhandled, string moviename) { this.onnetworkcallhandled = onnetworkcallhandled; this.moviename = moviename; } @override protected string doinbackground(void... params) { httpurlconnection urlconnection = null; bufferedreader reader = null; string moviejsonstr = null; try { final string movie_base_url = "http://www.omdbapi.com/?"; final string s = "s"; final string plot = "plot"; final string r = "r"; final string page = "page"; uri builturi = uri.parse(movie_base_url).buildupon() .appendqueryparameter(s, moviename) .appendqueryparameter(plot, "full") .appendqueryparameter(r, "json") .appendqueryparameter(page, "1") .build(); url url = new url(builturi.tostring()); urlconnection = (httpurlconnection) url.openconnection(); urlconnection.setrequestmethod("get"); urlconnection.connect(); inputstream inputstream = urlconnection.getinputstream(); stringbuffer buffer = new stringbuffer(); if (inputstream == null) { return null; } reader = new bufferedreader(new inputstreamreader(inputstream)); string line; while ((line = reader.readline()) != null) { buffer.append(line + "\n"); } if (buffer.length() == 0) { return null; } response = buffer.tostring(); } catch (exception e) { } return response; } @override protected void onpostexecute(string result) { super.onpostexecute(result); if (result != null || !result.equals("")) { onnetworkcallhandled.onnetworkcallsuccess(result); } else { onnetworkcallhandled.onnetworkcallfailure(true); } }
}
recyclerviewadapter class
public class recyclerviewadapter extends recyclerview.adapter<recyclerviewadapter.moviedetailsholder> { private arraylist<moviedetails> moviedetails; private context context; private onclicklistener onclick; recyclerviewadapter(arraylist<moviedetails> moviedetails, context context, onclicklistener onclick) { this.onclick = onclick; this.moviedetails = moviedetails; this.context = context; } @override public moviedetailsholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.card_view, parent, false); return new moviedetailsholder(view); } @override public void onbindviewholder(moviedetailsholder holder, final int position) { holder.tvmoviename.settext(moviedetails.get(position).gettitle()); picasso .with(context) .load(moviedetails.get(position).getposter()) .into(holder.ivposter); holder.setonclicklistener(onclick); } @override public int getitemcount() { return moviedetails.size(); } public static class moviedetailsholder extends recyclerview.viewholder implements view.onclicklistener{ private onclicklistener onclicklistener; cardview cv; textview tvmoviename; imageview ivposter; moviedetailsholder(view itemview) { super(itemview); cv = (cardview) itemview.findviewbyid(r.id.cardview); tvmoviename = (textview) itemview.findviewbyid(r.id.tvmoviename); ivposter = (imageview) itemview.findviewbyid(r.id.ivposter); itemview.setonclicklistener(this); } public void setonclicklistener(onclicklistener onclicklistener) { this.onclicklistener = onclicklistener; } @override public void onclick(view v) { onclicklistener.onclick(v, getposition(), getitemid()); } } public interface onclicklistener { public void onclick(view view, int position, long itemid); }
}
you treating moviefragment
activity
intent intent = new intent(this, moviefragment.class); intent.putextra(intent.extra_text, input); startactivity(intent);
but moviefragment
fragment. need add/replace moviefragment
in activity.
also, there no need setcontentview(r.layout.activity_main);
again in validateinput()
Comments
Post a Comment