Maintaning the state of checkbox on scroll in android -
what doing:
- i trying have checkbox in listview , trying main state onscroll
- i tried having boolean array state, couldnt achieve it....so ihave come model class
adptsearchfiltercategories .java
public class adptsearchfiltercategories extends baseadapter { private context context; list<modelfiltercategories> mdllst; public adptsearchfiltercategories(context context, list<modelfiltercategories> mdllst) { this.context = context; this.mdllst = mdllst; } @override public int getcount() { return mdllst.size(); } @override public object getitem(int position) { return mdllst.get(position); } @override public long getitemid(int position) { return position; } static class viewholder { protected checkbox chkbxcatid; } @override public view getview(int position, view convertview, viewgroup parent) { final modelfiltercategories lcldata = mdllst.get(position); viewholder viewholder = null; if (convertview == null) { layoutinflater minflater = (layoutinflater) context.getsystemservice(activity.layout_inflater_service); convertview = minflater.inflate(r.layout.adpt_searchfilter_category, null); viewholder = new viewholder(); viewholder.chkbxcatid = (checkbox) convertview.findviewbyid(r.id.chkbxcatid); viewholder.chkbxcatid.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { int getposition = (integer) buttonview.gettag(); // here position have set checkbox using settag. mdllst.get(getposition).setcheckboxstate(buttonview.ischecked()); // set value of checkbox maintain state. } }); convertview.settag(viewholder); } else { viewholder = (viewholder) convertview.gettag(); } viewholder.chkbxcatid.settag(position); // line important. viewholder.chkbxcatid.settext(lcldata.getcategoryname()); // txttitle.setid(integer.valueof(mapdata.get("id"))); viewholder.chkbxcatid.settag(lcldata.getcategoryid()); return convertview; } }
modelfiltercategories.java
public class modelfiltercategories { boolean checkboxstate; string categoryname; string categoryid; public boolean ischeckboxstate() { return checkboxstate; } public void setcheckboxstate(boolean checkboxstate) { this.checkboxstate = checkboxstate; } public string getcategoryname() { return categoryname; } public void setcategoryname(string categoryname) { this.categoryname = categoryname; } public string getcategoryid() { return categoryid; } public void setcategoryid(string categoryid) { this.categoryid = categoryid; } }
log:
12-16 17:00:16.000: e/androidruntime(9421): fatal exception: main 12-16 17:00:16.000: e/androidruntime(9421): process: com.windhyaworks, pid: 9421 12-16 17:00:16.000: e/androidruntime(9421): java.lang.classcastexception: java.lang.string cannot cast com.windhyaworks.adapters.adptsearchfiltercategories$viewholder 12-16 17:00:16.000: e/androidruntime(9421): @ com.windhyaworks.adapters.adptsearchfiltercategories.getview(adptsearchfiltercategories.java:69) 12-16 17:00:16.000: e/androidruntime(9421): @ android.widget.abslistview.obtainview(abslistview.java:2255) 12-16 17:00:16.000: e/androidruntime(9421): @ android.widget.listview.measureheightofchildren(listview.java:1263) 12-16 17:00:16.000: e/androidruntime(9421): @ android.widget.listview.onmeasure(listview.java:1175) 12-16 17:00:16.000: e/androidruntime(9421): @ android.view.view.measure(view.java:16540) 12-16 17:00:16.000: e/androidruntime(9421): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5137) 12-16 17:00:16.000: e/androidruntime(9421): @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1404) 12-16 17:00:16.000: e/androidruntime(9421): @ android.widget.linearlayout.measurevertical(linearlayout.java:695) 12-16 17:00:16.000: e/androidruntime(9421): @ android.widget.linearlayout.onmeasure(linearlayout.java:588) 12-16 17:00:16.000: e/androidruntime(9421): @ android.view.view.measure(view.java:16540) 12-16 17:00:16.000: e/androidruntime(9421): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5137) 12-16 17:00:16.000: e/androidruntime(9421): @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1404) 12-16 17:00:16.000: e/androidruntime(9421): @ android.widget.linearlayout.measurevertical(linearlayout.java:695) 12-16 17:00:16.000: e/androidruntime(9421): @ android.widget.linearlayout.onmeasure(linearlayout.java:588) 12-16 17:00:16.000: e/androidruntime(9421): @ android.view.view.measure(view.java:16540) 12-16 17:00:16.000: e/androidruntime(9421): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5137) 12-16 17:00:16.000: e/androidruntime(9421): @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1404) 12-16 17:00:16.000: e/androidruntime(9421): @ android.widget.linearlayout.measurehorizontal(linearlayout.java:1052) 12-16 17:00:16.000: e/androidruntime(9421): @ android.widget.linearlayout.onmeasure(linearlayout.java:590) 12-16 17:00:16.000: e/androidruntime(9421): @ android.view.view.measure(view.java:16540) 12-16 17:00:16.000: e/androidruntime(9421): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5137) 12-16 17:00:16.000: e/androidruntime(9421): @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1404) 12-16 17:00:16.000: e/androidruntime(9421): @ android.widget.linearlayout.measurehorizontal(linearlayout.java:1052) 12-16 17:00:16.000: e/androidruntime(9421): @ android.widget.linearlayout.onmeasure(linearlayout.java:590) 12-16 17:00:16.000: e/androidruntime(9421): @ android.view.view.measure(view.java:16540) 12-16 17:00:16.000: e/androidruntime(9421): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5137) 12-16 17:00:16.000: e/androidruntime(9421): @ android.widget.framelayout.onmeasure(framelayout.java:310) 12-16 17:00:16.000: e/androidruntime(9421): @ com.navdrawer.simplesidedrawer.onmeasure(simplesidedrawer.java:443) 12-16 17:00:16.000: e/androidruntime(9421): @ android.view.view.measure(view.java:16540) 12-16 17:00:16.000: e/androidruntime(9421): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5137) 12-16 17:00:16.000: e/androidruntime(9421): @ android.widget.framelayout.onmeasure(framelayout.java:310) 12-16 17:00:16.000: e/androidruntime(9421): @ com.android.internal.policy.impl.phonewindow$decorview.onmeasure(phonewindow.java:2291) 12-16 17:00:16.000: e/androidruntime(9421): @ android.view.view.measure(view.java:16540) 12-16 17:00:16.000: e/androidruntime(9421): @ android.view.viewrootimpl.performmeasure(viewrootimpl.java:1942) 12-16 17:00:16.000: e/androidruntime(9421): @ android.view.viewrootimpl.measurehierarchy(viewrootimpl.java:1132) 12-16 17:00:16.000: e/androidruntime(9421): @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1321) 12-16 17:00:16.000: e/androidruntime(9421): @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:1019) 12-16 17:00:16.000: e/androidruntime(9421): @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:5725) 12-16 17:00:16.000: e/androidruntime(9421): @ android.view.choreographer$callbackrecord.run(choreographer.java:761) 12-16 17:00:16.000: e/androidruntime(9421): @ android.view.choreographer.docallbacks(choreographer.java:574) 12-16 17:00:16.000: e/androidruntime(9421): @ android.view.choreographer.doframe(choreographer.java:544) 12-16 17:00:16.000: e/androidruntime(9421): @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:747) 12-16 17:00:16.000: e/androidruntime(9421): @ android.os.handler.handlecallback(handler.java:733) 12-16 17:00:16.000: e/androidruntime(9421): @ android.os.handler.dispatchmessage(handler.java:95) 12-16 17:00:16.000: e/androidruntime(9421): @ android.os.looper.loop(looper.java:136) 12-16 17:00:16.000: e/androidruntime(9421): @ android.app.activitythread.main(activitythread.java:5086) 12-16 17:00:16.000: e/androidruntime(9421): @ java.lang.reflect.method.invokenative(native method) 12-16 17:00:16.000: e/androidruntime(9421): @ java.lang.reflect.method.invoke(method.java:515) 12-16 17:00:16.000: e/androidruntime(9421): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:785) 12-16 17:00:16.000: e/androidruntime(9421): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:601) 12-16 17:00:16.000: e/androidruntime(9421): @ dalvik.system.nativestart.main(native method)
try
// getview
if (convertview == null) { convertview = minflater .inflate(r.layout.filter_list_view, null); holder = new viewholder(); holder.title = (textview) convertview.findviewbyid(r.id.title); holder.checkbox = (checkbox) convertview .findviewbyid(r.id.checkbox); holder.checkbox.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { int getposition = (integer) buttonview.gettag(); // here position have set checkbox using settag. ((filterlistbean) itemlist.get(getposition)).setselected(buttonview.ischecked()); // had use casting since method required,set value of checkbox maintain state. } }); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.checkbox.settag(position); holder.title.settext(menulist.gettitle()); holder.checkbox.setchecked( menulist.getselected());
//within constructor of adapter
for (int = 0; < this.getcount(); i++) { log.v("count---", getcount() + ""); itemchecked.add(i, false); // initializes items value false }
// adapter class variable list
private arraylist<boolean> itemchecked = new arraylist<boolean>();
Comments
Post a Comment