javascript - Listview containing checkbox and text is getting reset on scrolling -
i have drawn customized navigation drawer listview , header when scroll list checkbox in list getting unchecked.
secondly when click on reset button in header part want checkbox in listview should get unchecked. have been trying working unable find solution..
the snippets are
public class navigationdrawer extends fragment{ @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.filter_navigation_drawer, container,false); drawerlistview= ( listview ) view.findviewbyid( r.id.listdrawer ); drawerlistview.setonitemclicklistener(new filterdraweritemclicklistener()); datalist.add(new filterdraweritem("sample1",true)); datalist.add(new filterdraweritem("sample2",true)); datalist.add(new filterdraweritem("sample3",true)); datalist.add(new filterdraweritem("sample4",true)); datalist.add(new filterdraweritem("sample5",true)); datalist.add(new filterdraweritem("sample2",true)); datalist.add(new filterdraweritem("sample2",true)); datalist.add(new filterdraweritem("sample2",true)); datalist.add(new filterdraweritem("sample2",true)); adapter = new filtercustomdraweradapter(getactivity(), r.layout.drawer_filter,datalist,drawerstatus); drawerlistview.setadapter(adapter); adapter.getfilterlist(); resetbtn = (textview)view.findviewbyid(r.id.filterby_reset); if(resetbtn != null){ resetbtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { application.setfilterstatus("reset"); for(int i=0; i<datalist.size(); i++){ datalist.get(i).setcheckboxid(false); } adapter.notifydatasetchanged(); // this.oncreateview(); } }); } return view; } }
filtercustomdraweradapter.java
public class filtercustomdraweradapter extends arrayadapter<filterdraweritem> { context context; list<filterdraweritem> draweritemlist; int layoutresid; int item = 0; string status; list<integer> filterlist = new arraylist<integer>(); drawerstatus drawerstatus; stataapplication application = stataapplication.getinstance(); hashmap<integer, boolean> checked; // newly added code public filtercustomdraweradapter(context context, int layoutresourceid, list<filterdraweritem> listitems,drawerstatus drawerstatus) { super(context, layoutresourceid, listitems); this.context = context; this.draweritemlist = listitems; this.layoutresid = layoutresourceid; this.drawerstatus = drawerstatus; checked = new hashmap<integer, boolean>(getcount()); } public filtercustomdraweradapter(context context, int layoutresourceid, list<filterdraweritem> listitems) { super(context, layoutresourceid, listitems); this.context = context; this.draweritemlist = listitems; this.layoutresid = layoutresourceid; } @override public view getview(final int position, view convertview, viewgroup parent) { // todo auto-generated method stub final filterdraweritemholder drawerholder; view view = convertview; if (view == null) { layoutinflater inflater = ((activity) context).getlayoutinflater(); drawerholder = new filterdraweritemholder(); view = inflater.inflate(layoutresid, parent, false); drawerholder.itemname = (textview) view.findviewbyid(r.id.drawer_filtername); drawerholder.checkbox = (checkbox) view.findviewbyid(r.id.drawer_cbox); view.settag(drawerholder); } else { drawerholder = (filterdraweritemholder) view.gettag(); } filterdraweritem ditem = (filterdraweritem) this.draweritemlist.get(position); drawerholder.itemname.settext(ditem.getitemname()); textview resetview = (textview)view.findviewbyid(r.id.filterby_reset); checkbox checkbox = (checkbox) view.findviewbyid(r.id.drawer_cbox); // newly added code boolean ischecked = checked.get(position); checkbox.setchecked(ischecked == null ? false : ischecked); // if(application.getfilterstatus() != null) { if(checkbox.ischecked()){ drawerholder.checkbox.setchecked(false); } // } checkbox.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview,boolean ischecked) { if (ischecked) { filterlist.add(new integer(position)); checked.put(position, true); } else { filterlist.remove(new integer(position)); checked.put(position, false); } } }); drawerholder.checkbox.settag(position); log.d("filter_list_size",string.valueof(filterlist.size())); return view; } private static class filterdraweritemholder { textview itemname; checkbox checkbox; } public list<integer> getfilterlist(){ return filterlist; } }
in image below when scroll list , if make checkbox sample1 , sample 2 checked becomes unchecked on scrolling. , on clicking reset button in header want checkbox unchecked.. not able working ...
update 1
resetbtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { list<filterdraweritem> adapterdatalist = adapter.getdraweritemlist(); for(int i=0; i<adapterdatalist.size(); i++){ // @ place getting size 9 adapterdatalist.get(i).setcheckboxid(false); } adapter.setdraweritemlist(adapterdatalist); adapter.notifydatasetchanged(); } });
it because of recycling use of views in listview. should create hashmap:
hashmap<integer, boolean> checked;
then in constructor this:
checked = new hashmap<integer, boolean>(getcount());
after set oncheckedchangelistener on checkboxes, , in event method this:
checked.put(position, yourcheckboxcheckedstate);
and in getview() method this:
boolean ischecked = checked.get(position); checkbox.setchecked(ischecked == null ? false : ischecked)
try this, think should help
update
resetbtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { adapter.deselectall(); } });
then in adapter create method:
public void deselectall() { checked = new hashmap<integer, boolean>(getcount()); notifydatasetchanged(); }
Comments
Post a Comment