android - Change background colour of listview items based on cursor results -
i have following listview..
private void populatekpilistview(string storedstaffid, view view, string id){ //list 1 cursor cursor = db.getallrows("*", dbtables.table_kpis, "where projectid=" + id); int count = db.getrowcount("*", dbtables.table_kpis, "where projectid=" + id); textview empty = (textview)view.findviewbyid(android.r.id.empty); if(count > 0){ empty.setvisibility(view.gone); string[] fromfieldnames = new string[] {dbtables.key_kpiheader, dbtables.key_target, dbtables.key_actual}; int[] toviewids = new int[] { r.id.card_title, r.id.card_target, r.id.card_actual}; mycustomadaptor = new customlistadapter(getactivity(), r.layout.kpi_list_item_card, cursor, fromfieldnames, toviewids, 0); kpilist = (listview)view.findviewbyid(android.r.id.list); kpilist.setadapter(mycustomadaptor); kpilist.setonitemclicklistener(new onitemclicklistener(){ @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { intent = new intent(getactivity(), kpipopupactivity.class); i.putextra("db_id", id); startactivity(i); } }); }else{ empty.setvisibility(view.visible); empty.settext(r.string.kpi_empty_string); } }
what need change colour of individual list item if 'target' lower 'actual' in cursor. created simple custom list adapter...
public class customlistadapter extends simplecursoradapter { private int mselectedposition; cursor items; private context context; private int layout; public customlistadapter(context context, int layout, cursor c, string[] from, int[] to, int num) { super(context, layout, c, from, to, num); this.context = context; this.layout = layout; } @override public view getview(int position, view convertview, viewgroup parent) { view v = super.getview(position, convertview, parent); cursor c = getcursor(); c.movetoposition(position); int target = c.getcolumnindex(dbtables.key_target); int actual = c.getcolumnindex(dbtables.key_actual); relativelayout rellay = (relativelayout)v.findviewbyid(r.id.kpi_box); log.i(""+target, actual+""); if (actual > target) { // set background color of text. rellay.setbackgroundcolor(v.getresources().getcolor(r.color.urgent)); } else { rellay.setbackgroundcolor(v.getresources().getcolor(r.color.white)); } return v; } }
the problem changes list items red. log produces same produces same result every item (hence them being red). says every item has target of 7 , actual of 8. in reality know none of results have actuals or targets these numbers.
the problem is, you're getting column index , not value in column in row. try following:
int target = c.getint(c.getcolumnindex(dbtables.key_target)); int actual = c.getint(c.getcolumnindex(dbtables.key_actual));
Comments
Post a Comment