Custom ListView with checkbox changes position while scrolling in android -
i new android , working on web view demo, have implemented it. problem me when check check box , scrolling list view , checked position changes.
i have tried links stack on flow no luck, request can me resolve issue, please adapter below,
adapter
public class filteradapter extends baseadapter { private static final int type_item = 0; private static final int type_separator = 1; private static final int type_max_count = type_separator + 1; private arraylist<string> mdata = new arraylist<string>(); private layoutinflater minflater; private treeset<integer> mseparatorsset = new treeset<integer>(); public filteradapter(context context) { minflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); } public void additem(final string item) { mdata.add(item); notifydatasetchanged(); } public void addseparatoritem(final string item) { mdata.add(item); // save separator position mseparatorsset.add(mdata.size() - 1); notifydatasetchanged(); } @override public int getitemviewtype(int position) { return mseparatorsset.contains(position) ? type_separator : type_item; } @override public int getviewtypecount() { return type_max_count; } public int getcount() { return mdata.size(); } public string getitem(int position) { return mdata.get(position); } public long getitemid(int position) { return position; } public view getview(int position, view convertview, viewgroup parent) { viewholder holder = null; int type = getitemviewtype(position); system.out.println("getview " + position + " " + convertview + " type = " + type); if (convertview == null) { holder = new viewholder(); switch (type) { case type_item: convertview = minflater.inflate(r.layout.raw_filter, null); holder.textview = (textview) convertview.findviewbyid(r.id.tv_filter); holder.chk_filter = (checkbox) convertview.findviewbyid(r.id.chk_filter); break; case type_separator: convertview = minflater.inflate(r.layout.raw_headr, null); holder.textview = (textview) convertview.findviewbyid(r.id.tv_hdr); break; } convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.textview.settext(mdata.get(position)); return convertview; } } class viewholder { public textview textview; public checkbox chk_filter; }
add clicklistener
in getview()
method, fix set checked box in entire listview
this:
checkboxselected.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { instrumentclicklistener.oncheckboxclick(instrumentdto, checkboxselected.ischecked(), position); log.d(tag, "checked instruments check: " + checkboxselected.ischecked() + " \ninstrument: " + position); } });
Comments
Post a Comment