java - Lost item background color when scrolling listview -
i tried used solutions stackoverflow, without succesfull. so, set background color item after click - working. after scrolling (when item out off screen) , scrolling (item in screen), item lost background color. here part of code:
public class rowadapter extends arrayadapter<filerow> { private final activity context; private final list<filerow> list; public rowadapter(activity context, list<filerow> list) { super(context, r.layout.browser_row, list); this.context = context; this.list = list; } @override public view getview(int position, view convertview, viewgroup parent) { view v = super.getview(position, convertview, parent); layoutinflater inflator = context.getlayoutinflater(); convertview = inflator.inflate(r.layout.browser_row, null); textview textview = (textview)convertview; textview.settext(list.get(position).getname()); if (!list.get(position).getname().contains("/")) { textview.setpadding(5, 5, 5, 5); textview.setcompounddrawableswithintrinsicbounds(0,0,0,0); } (int i=0; i<browserfiles.selecteditems.size(); i++) { if (browserfiles.selecteditems.get(i).getposition() == position) { v.setbackgroundcolor(color.rgb(255, 165, 0)); break; } } return convertview; }
}
public class browserfiles extends listactivity implements onitemclicklistener { private string lastdir = "/"; private list<filerow> items = null; public static list<filerow> selecteditems; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.browser_files); listview lv = (listview) findviewbyid(android.r.id.list); lv.setonitemclicklistener(this); intent = getintent(); lastdir = i.getstringextra("lastdir"); getdir(lastdir); selecteditems = new arraylist<filerow>(); } @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { textview tv = (textview)view; //jedna se o soubor if (tv.gettext().tostring().tolowercase(locale.english).endswith("mp3")) { string item = lastdir + tv.gettext().tostring(); int isinlist = -1; (int i=0; < selecteditems.size(); i++) { if (selecteditems.get(i).getname().comparetoignorecase(item) == 0) { isinlist = i; break; } } if (isinlist != -1) { selecteditems.get(isinlist).setselected(false); selecteditems.remove(isinlist); parent.getchildat(position).setselected(false); parent.getchildat(position).setbackgroundcolor(color.transparent); } else { selecteditems.add(new filerow(item, true, position)); parent.getchildat(position).setselected(true); parent.getchildat(position).setbackgroundcolor(color.rgb(255, 165, 0)); } } //jedna se o adresar else { getdir(lastdir.compareto("/") == 0 ? lastdir + tv.gettext().tostring() : lastdir + "/" + tv.gettext().tostring()); } }
i found solutions. add 2 lines:
for (int i=0; i<browserfiles.selecteditems.size(); i++) { if (browserfiles.selecteditems.get(i).getposition() == position) { v.setbackgroundcolor(color.rgb(255, 165, 0)); if (convertview != null) convertview.setbackgroundcolor(color.rgb(255, 165, 0)); break; } }
Comments
Post a Comment