java - ListView Viewholder checkbox state -
i've problems listview custom adapter (and newly implemented viewholder). have listview checkbox each item (nothing new here). problem is, if there more 9 items in list, when check first checkbox, tenth automatically checked (same second eleventh) if there 1 listener both item (and beleive it's case in way).
i read position issue listview, view recycling , viewholder way solve here: how can make arrayadapter follow viewholder pattern?
but made wrong because it's not working...
public class presencelistadapter extends simpleadapter { private layoutinflater inflater; private list<integer> ids; private list<string> statuts; public presencelistadapter (context context, list<? extends map<string, ?>> data, int resource, string[] from, int[] to, list<integer> ids, list<string> statuts) { super (context, data, resource, from, to); inflater = layoutinflater.from (context); this.ids = ids; this.statuts= statuts; } @override public object getitem (int position) { return super.getitem (position); } @override public view getview (int position, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { convertview = inflater.inflate (r.layout.list_text_checkbox, null); holder = new viewholder(); holder.btn = (button) convertview.findviewbyid(r.id.btnretard); holder.check = (checkbox) convertview.findviewbyid(r.id.checkpresent); if (statuts.get(position).equals("p")) { drawable img = inflater.getcontext().getresources().getdrawable(android.r.drawable.presence_online); holder.btn.setcompounddrawableswithintrinsicbounds( img, null, null, null ); holder.btn.setenabled(true); holder.check.setchecked(true); } else if(statuts.get(position).equals("r")) { drawable img = inflater.getcontext().getresources().getdrawable(android.r.drawable.presence_away); holder.btn.setcompounddrawableswithintrinsicbounds( img, null, null, null ); holder.btn.setenabled(true); holder.check.setchecked(true); } else { drawable img = inflater.getcontext().getresources().getdrawable(android.r.drawable.presence_invisible); holder.btn.setcompounddrawableswithintrinsicbounds( img, null, null, null ); holder.check.setchecked(false); } convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } int id = ids.get(position); if(id != 0) { holder.check.settag(id); holder.btn.settag(id); } return super.getview (position, convertview, parent); } static class viewholder { button btn; checkbox check; }
and listener: public void changerpresent(view v) {
checkbox checkpresent = (checkbox) v; int idpersonne = (integer) checkpresent.gettag(); view parent = (view)v.getparent(); button btn = (button) parent.findviewbyid(r.id.btnretard); if(checkpresent.ischecked()) { gestion.updatepresence(idpersonne, idseance, "p"); btn.setenabled(true); setbtnretardpresent(btn); } else { gestion.updatepresence(idpersonne, idseance, "a"); btn.setenabled(false); setbtnretardabsent(btn); } }
i appreciate @ point, i'm working on hours now.
thank much.
here's how made work:
first, need separate array checked state. has same size adapter's getcount()
.
then on getview, checkbox's setoncheckedchangedlistener must preceed checkbox.setchecked statements.
example:
holder.checkbox.setoncheckedchangelistener(new oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { ischecked[position] = ischecked; } }); holder.checkbox.setchecked(ischecked[position]);
Comments
Post a Comment