Featured post
android - ListView color issue -
hey i'm not professional coder stretch, i've been playing around simple to-do list learn basics of android development.
i've been able want working, there 1 problem listview has me totally stumped. i've extended simplecursoradapter in order format data coming sqlite database , change color of duedate text based on whether or not duedate has expired. formatting works flawlessly, i'm getting odd results colors.
the first few entries in listview expect them to, scroll down, inconsistencies start popping items no due date set colored red or green. more scroll , down in list, more inconsistencies appear until every row colored whether should or not.
can me understand going on here? please see custom adapter below.
public class proadapter2 extends simplecursoradapter { private static int[] = {r.id.priority, r.id.projectname, r.id.duedate}; private static string[] = {"priorities", "projectname", "duedate"}; private context context; private int layout; //constructor public proadapter2(context context, int layout, cursor c) { super(context,layout, c, from, to); this.context=context; this.layout = layout; } @override public view newview(context context, cursor curso, viewgroup parent){ cursor c = getcursor(); final layoutinflater inflater = layoutinflater.from(context); view v = inflater.inflate(layout, parent, false); textview txtname = (textview) v.findviewbyid(r.id.projectname); txtname.settext(c.getstring(1)); textview txtpriority = (textview) v.findviewbyid(r.id.priority); txtpriority.settext(c.getstring(2)); return v; } @override public void bindview(view v, context context, cursor c) { textview txtduedate = (textview) v.findviewbyid(r.id.duedate); textview txtduedatelabel = (textview) v.findviewbyid(r.id.duedate_label); textview txtpriority = (textview) v.findviewbyid(r.id.priority); textview txtprioritylabel = (textview) v.findviewbyid(r.id.priority_label); textview txtname = (textview) v.findviewbyid(r.id.projectname); linearlayout pridate = (linearlayout) v.findviewbyid(r.id.pridate); string duedate = c.getstring(3); string cdate = c.getstring(4); string duedateformated; mytime t = new mytime(); long ctimelong = c.getlong(6); long dtimelong = c.getlong(5); duedateformated = t.getformatedtime(c.getstring(3)); txtduedate.settext(duedateformated); if (c.getint(5)==0){ txtduedate.settext("not set"); } else if (ctimelong < dtimelong){ txtduedate.settextcolor(color.green); } else if (ctimelong > dtimelong){ txtduedate.settextcolor(color.red); } } }
the simplest solution add following after txtduedate.settext("not set");
:
txtduedate.settextcolor(color.black);
the reason listview
recycles view
objects create in newview
method.
so when bindview
called, v
parameter isn't shiny-new view
object, in fact 1 top of list isn't visible more, , had green or red text.
so need make sure explicitly set properties require, including text colour.
aside that, there further optimisations can make. example, shouldn't calling settext
in newview
method — should in bind method.
for info how listview
works, can watch talk or @ pdf slides:
http://www.google.com/events/io/2010/sessions/world-of-listview-android.html
- Get link
- X
- Other Apps
Comments
Post a Comment