java - CheckBox state inside RecyclerView -
i created checkbox, however, whenever click few items random checkboxes become clicked. have recycler view, , have adapter class well. here checkbox method can tell me problem?
public void checkbox(view view) { final checkbox checkbox = (checkbox)view; if (checkbox.ischecked()) { system.out.println("set checked"); //input instance of selected course(checked) // todo: 2/5/16 need pass in persisted id mvolleysingleton = volleysingleton.getinstance(); mrequestqueue = mvolleysingleton.getrequestqueue(); charsequence id_put_courses = ((textview) ((relativelayout) view.getparent()).getchildat(1)).gettext(); system.out.println(put); string url_put_courses = "url"+id_put+"\n"; system.out.print(put); stringrequest urlput = new stringrequest(request.method.put, url_put, new response.listener<string>() { @override public void onresponse(string response) { system.out.println(response + "reponse"); } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { error.printstacktrace(); system.out.println("************answer" + error + "error"); } }); mrequestqueue.add(urlputcourses); system.out.println("added"); } else{ system.out.println("set unchecked"); //delete instance of selected course(unchecked) mvolleysingleton = volleysingleton.getinstance(); mrequestqueue = mvolleysingleton.getrequestqueue(); // todo: 2/5/16 need pass in persisted id charsequence delete = ((textview) ((relativelayout) view.getparent()).getchildat(1)).gettext(); system.out.print(id_put_courses); string ur_delete = "url"+ delete; stringrequest urldeletecourses = new stringrequest(request.method.delete, ur_delete, new response.listener<string>() { @override public void onresponse(string response) { system.out.println(response + "reponse"); } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { error.printstacktrace(); system.out.println("************answer" + error + "error"); } }); mrequestqueue.add(ur_delete); system.out.println("deleted"); } } ublic class adaptersearch extends recyclerview.adapter<adaptersearch.viewsearch>{ private layoutinflater mlayoutinflater; private arraylist<search> listsearch=new arraylist<>(); public adaptersearch(context context){ mlayoutinflater=layoutinflater.from(context); } public void setlistsearch(arraylist<search> listsearch){ this.listsearch=listsearch; notifyitemrangechanged(0,listsearch.size()); } @override public viewsearch oncreateviewholder(viewgroup parent, int viewtype) { view view= mlayoutinflater.inflate(r.layout.custom_search,(viewgroup)null); viewsearch holder=new viewsearch(view); return holder; } @override public void onbindviewholder(viewsearch holder, int position) { search currentsearch=listsearch.get(position); holder.msearchtext.settext(currentsearch.getmtitle()); holder.manswerpointssearch.settext(currentsearch.getmkey()); holder.msearchid.settext(currentsearch.getmid()); holder.mcourseid.settext(currentsearch.getcourseid()); } @override public int getitemcount() { return listsearch.size(); } public void setfilter(arraylist<search> search) { listsearch = new arraylist<>(); listsearch.addall(search); notifydatasetchanged(); } static class viewsearch extends recyclerview.viewholder{ private textview msearchtext; private textview manswerpointssearch; private textview msearchid; private textview mcourseid; public viewsearch (view view){ super(view); msearchtext=(textview)itemview.findviewbyid(r.id.searchtext); manswerpointssearch=(textview)itemview.findviewbyid(r.id.answerpointssearch); msearchid=(textview)itemview.findviewbyid(r.id.searchid); mcourseid=(textview)itemview.findviewbyid(r.id.coursetextview); } }
}
better late never i'd say, might other people stumbling upon issue later, here goes.
this might not best solution ever, should work.
in search
class, have variables title
, key
. add boolean
ischecked
it, , make false
default. create matching getter , setter. (i'm using ischecked()
& setchecked()
in example)
in onbindviewholder()
method, add following lines:
checkbox checkbox = (checkbox) itemview.findviewbyid(r.id.checkboxid); // instantiating checkbox holder.checkbox.setchecked(currentsearch.ischecked()); // every time holder (one list item) shown, gets called. // sets checked state based on what's stored in currentsearch. checkbox.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (checkbox.ischecked()) { currentsearch.setchecked(true) } else { currentsearch.setchecked(false); } }); } // listens clicks on checkbox, , changes checked state of // boolean in currentsearch correct state based on // on.
please aware haven't worked checkboxes before, things (like setting checked state) might done differently i've described above, should working base.
you want checkboxes persevere until click button or that, , have them reset. might want add simple loop that, like:
public void resetallcheckboxes() { (search s : listsearch) { s.setchecked(false); } }
Comments
Post a Comment