Friday, 15 April 2011

listview - Android ViewBinder values from cursor -



listview - Android ViewBinder values from cursor -

hoping can help me here. have layout contains listview, each item in list view contains individual text fields represent days of week --> smtwtfs trying alter colour of text items if set within database. works have noticed first row in list view changes colour items when set in other list rows.

my text items declared this

textsun = (textview) v.findviewbyid(r.id.textsun); textmon = (textview) v.findviewbyid(r.id.textmon); texttue = (textview) v.findviewbyid(r.id.texttue); textwed = (textview) v.findviewbyid(r.id.textwed); textthu = (textview) v.findviewbyid(r.id.textthu); textfri = (textview) v.findviewbyid(r.id.textfri); textsat = (textview) v.findviewbyid(r.id.textsat);

i query db , set adapter

db = new databasehandler(getactivity()); mcursor=db.getreadabledatabase().rawquery("select rowid _id,* "+ "from table", null); adapter = new simplecursoradapter(getactivity(), r.layout.list_row, mcursor, new string[] {databasehandler.key_sunday, databasehandler.key_monday,databasehandler.key_tuesday,databasehandler.key_wednesday, databasehandler.key_thursday, databasehandler.key_friday, databasehandler.key_saturday,}, new int[] {r.id.textsun,r.id.textmon, r.id.texttue, r.id.textwed, r.id.textthu, r.id.textfri, r.id.textsat});

finally seek set color in view binder if value returned 1 db

adapter.setviewbinder(new simplecursoradapter.viewbinder() { @override public boolean setviewvalue(view view, cursor cursor, int columnindex) { final int sun_column = cursor.getcolumnindex("sun"); final int mon_column = cursor.getcolumnindex("mon"); final int tue_column = cursor.getcolumnindex("tue"); final int wed_column = cursor.getcolumnindex("wed"); final int thu_column = cursor.getcolumnindex("thu"); final int fri_column = cursor.getcolumnindex("fri"); final int sat_column = cursor.getcolumnindex("sat"); } if (columnindex == sun_column) { int sun = cursor.getint(cursor.getcolumnindexorthrow("sun")); if(sun == 1){ ((textview) view).settextcolor(color.parsecolor("#ffffff")); } homecoming true; } if (columnindex == mon_column) { int mon = cursor.getint(cursor.getcolumnindexorthrow("mon")); if(mon == 1){ ((textview) view).settextcolor(color.parsecolor("#ffffff")); } homecoming true; } if (columnindex == tue_column) { int tue = cursor.getint(cursor.getcolumnindexorthrow("tue")); if(tue == 1){ ((textview) view).settextcolor(color.parsecolor("#ffffff")); } homecoming true; } if (columnindex == wed_column) { int wed = cursor.getint(cursor.getcolumnindexorthrow("wed")); if(wed == 1){ ((textview) view).settextcolor(color.parsecolor("#ffffff")); } homecoming true; } if (columnindex == thu_column) { int thu = cursor.getint(cursor.getcolumnindexorthrow("thu")); if(thu == 1){ ((textview) view).settextcolor(color.parsecolor("#ffffff")); } homecoming true; } if (columnindex == fri_column) { int fri = cursor.getint(cursor.getcolumnindexorthrow("fri")); if(fri == 1){ ((textview) view).settextcolor(color.parsecolor("#ffffff")); } homecoming true; } if (columnindex == sat_column) { int sat = cursor.getint(cursor.getcolumnindexorthrow("sat")); if(sat == 1){ ((textview) view).settextcolor(color.parsecolor("#ffffff")); } homecoming true; } homecoming false; } }); lv = (listview) v.findviewbyid(r.id.listview); lv.setadapter(adapter);

perhaps going wrong ?? works though.

many thanks

you still need handle cases mon, tue, etc. == 0. adding next each if statement should prepare it:

else { ((textview) view).settextcolor(//color want if day == 0); }

also, may want consider changing whole setviewvalue utilize switch statement, should create cod more readable.

android listview cursor android-viewbinder

No comments:

Post a Comment