android - How to maintain position of list view row/item? -
i have custom listview
containing textview
, imageview
, checkbox
. in listview
, nothing should checked/unchecked have block based on conditions.
tasks doing:
if
checkbox
checked should show image inimageview
.i don't want make
checkbox
checkable/enabled based on conditions.
below code custom adapter.
public class customadapter extends baseadapter { private context context; private arraylist<pojo> maindatalist; private layoutinflater inflater = null; public customadapter(context context, arraylist< pojo > maindatalist) { this.context = context; this.maindatalist = maindatalist; inflater = ( layoutinflater ) context.getsystemservice(context.layout_inflater_service); } @override public int getcount() { // todo auto-generated method stub return maindatalist.size(); } @override public long getitemid(int position) { // todo auto-generated method stub return position; } @override public pojo getitem(int position) { // todo auto-generated method stub return maindatalist.get(position); } public class holder { private textview name; private imageview imageview; private checkbox check; } @override public view getview(final int position, view view, viewgroup parent) { // todo auto-generated method stub final holder holder; if (view == null) { holder = new holder(); view = inflater.inflate(r.layout.my_layout_file, null); holder.name = (textview) view.findviewbyid(r.id.textview_name); holder.imageview = (imageview) view.findviewbyid(r.id.img); holder.check = (checkbox) view.findviewbyid(r.id.checkbox); view.settag(holder); } else { holder = (holder) view.gettag(); } holder.check.settag(position); if (maindatalist.get(position).iseditable()) { holder.check.setenabled(false); holder.check.setclickable(false); } else { holder.check.setenabled(true); holder.check.setclickable(true); } } holder.name.settext(maindatalist.get(position).getname()); holder.check.setchecked(maindatalist.get(position).isselected()); if (maindatalist.get(position).isselected()) holder.imageview.setimageresource(r.drawable.ic_launcher); else holder.imageview.setimageresource(null); return view; } }
my problems are:
on scroll,
imageview
shows images randomly irrespective of true condition.on scroll, after every 4-5 list items/rows item non-checkable (my device shows 4-5 cells/rows/items on screen without scroll) item 1 non-checkable. simply, retrieves per getview() mechanism.
i know problem of maintaining list item position or listview's recycling on scroll. how this?
you have condition such as:
if (maindatalist.get(position).isselected()) holder.imageview.setimageresource(r.drawable.ic_launcher);
which means if model has property selected true
set image resource image view. problem lies beneath fact, whenever scroll , isselected()
yields false
, not anything. because re-using views, getting old views @ point have had isselected()
true. might false
, haven't specified condition that.
my suggestion in terms of:
if (maindatalist.get(position).isselected()) { holder.imageview.setimageresource(r.drawable.ic_launcher); } else { holder.imageview.setimageresource(null); }
Comments
Post a Comment