Saturday, 15 January 2011

android - text color passes to recycled list items in my adapter -



android - text color passes to recycled list items in my adapter -

i'm trying alter text colour of textview within listitem if text number under 5. when scroll / down, colours apply recycled items. i've looked around , don't understand how these recycled items work.

fragment class

listadapter adapter = new racesadapter(getactivity(), raceslist, r.layout.list_item, new string[] { tag_name, tag_goal, tag_numentrants }, new int[] { r.id.gamelabel, r.id.goallabel, r.id.entrantslabel}); setlistadapter(adapter);

racesadapter class (is modified simpleadapter) (look in bindview() code)

public view getview(int position, view convertview, viewgroup parent) { homecoming createviewfromresource(position, convertview, parent, mresource); } private view createviewfromresource(int position, view convertview, viewgroup parent, int resource) { view v; if (convertview == null) { v = minflater.inflate(resource, parent, false); } else { v = convertview; } bindview(position, v); homecoming v; } public void setdropdownviewresource(int resource) { this.mdropdownresource = resource; } @override public view getdropdownview(int position, view convertview, viewgroup parent) { homecoming createviewfromresource(position, convertview, parent, mdropdownresource); } @suppresswarnings("rawtypes") private void bindview(int position, view view) { final map dataset = mdata.get(position); if (dataset == null) { return; } final string[] = mfrom; final int[] = mto; final int len = to.length; (int = 0; < len; i++) { final view v = view.findviewbyid(to[i]); if (v != null) { final object info = dataset.get(from[i]); string text = info == null ? "" : data.tostring(); if (text == null) { text = ""; } boolean bound = false; if (mviewbinder != null) { bound = mviewbinder.setviewvalue(v, data, text); } if (!bound) { if (v instanceof textview) { if(v.getid()==r.id.entrantslabel){ if(integer.parseint(text) < 5){((textview) v).settextcolor(color.red);} } setviewtext((textview) v, text); } else if (v instanceof imageview) { if (data instanceof integer) { setviewimage((imageview) v, (integer) data); } else { setviewimage((imageview) v, text); } } else { throw new illegalstateexception(v.getclass().getname() + " not " + " view can bounds simpleadapter"); } } } } } public viewbinder getviewbinder() { homecoming mviewbinder; }

what should is, in bindview(), set text color default. then, may or may not alter color.red. if does, it's red, otherwise, it's default.

so, this:

private void bindview(int position, view view) { // ... (int = 0; < len; i++) { final view v = view.findviewbyid(to[i]); if (v != null) { v.settextcolor(color.black); // illustration // ... if (!bound) { if (v instanceof textview) { if(v.getid()==r.id.entrantslabel){ if(integer.parseint(text) < 5){((textview) v).settextcolor(color.red);} } // ... } // ... } } } }

android android-listview android-textview simplecursoradapter

No comments:

Post a Comment