android - setOnItemClickListener in ListView affecting multiple rows -
i have custom listview , custom adapter. when clicking row of listview, text of textview set white , background of row set black.
all when clicking row, other rows having background color set black , when scrolling , down mess up.
getview() in custom adapter
public view getview(final int position, view convertview, viewgroup parent) { final listitem holder; view vi=convertview; if(vi==null){ vi = inflater.inflate(r.layout.list, null); holder = new listitem(); holder.nametext= (textview) vi.findviewbyid(r.id.name); vi.settag(holder); }else{ holder = (listitem) vi.gettag(); } holder.nametext.settext(""+item.name); return vi; }
in mainactivity:
customadapter listadapter = new customadapter(context, r.layout.list, items); list.setadapter(listadapter); list.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> adapter, view view, int position, long arg){ textview v = (textview) view.findviewbyid(r.id.name); view.setbackgroundcolor(color.black); v.settextcolor(color.white); } });
i tried puttin setonitemclicklistener in getview() no luck either..
any help? thanks.
custom listviews "create" view showing rows only, when scrolling down next showing row affected first one..
to prevent issue, need create boolean field in listitem clicked;
then when clicking view, item.clicked set true in getview()
final listitem item = items.get(position);
and
list<listitem> items;
as adapter's variable
the implementation:
you need set setonclicklistener in getview() below
vi.setonclicklistener(new onclicklistener(){ @override public void onclick(view arg1) { if(previousview!=null){ listitem previousitem = items.get(previousposition); previousitem.clicked = false; } item.clicked = true; previousview = arg1; previousposition = position; notifydatasetchanged(); } }); if(!item.isclicked){ holder.nametext.settextcolor(color.black); vi.setbackgroundcolor(color.white); }else{ holder.nametext.settextcolor(color.white); vi.setbackgroundcolor(color.black); } });
Comments
Post a Comment