Reputation: 25
Hoping someone can help me here. I have a layout which contains listview, each item in the list view contains individual text fields to represent the days of the week --> SMTWTFS What I am trying to do is change the colour of the text items if they are set within the database. This nearly works but I have noticed the first row in the list view changes the colour for all items when the are set in other list rows.
My text items are declared like 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 then query my db and set the 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 I try to set the color in the view binder if the value returned is 1 from the 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"));
}
return true;
}
if (columnIndex == mon_column) {
int mon = cursor.getInt(cursor.getColumnIndexOrThrow("mon"));
if(mon == 1){
((TextView) view).setTextColor(Color.parseColor("#FFFFFF"));
}
return true;
}
if (columnIndex == tue_column) {
int tue = cursor.getInt(cursor.getColumnIndexOrThrow("tue"));
if(tue == 1){
((TextView) view).setTextColor(Color.parseColor("#FFFFFF"));
}
return true;
}
if (columnIndex == wed_column) {
int wed = cursor.getInt(cursor.getColumnIndexOrThrow("wed"));
if(wed == 1){
((TextView) view).setTextColor(Color.parseColor("#FFFFFF"));
}
return true;
}
if (columnIndex == thu_column) {
int thu = cursor.getInt(cursor.getColumnIndexOrThrow("thu"));
if(thu == 1){
((TextView) view).setTextColor(Color.parseColor("#FFFFFF"));
}
return true;
}
if (columnIndex == fri_column) {
int fri = cursor.getInt(cursor.getColumnIndexOrThrow("fri"));
if(fri == 1){
((TextView) view).setTextColor(Color.parseColor("#FFFFFF"));
}
return true;
}
if (columnIndex == sat_column) {
int sat = cursor.getInt(cursor.getColumnIndexOrThrow("sat"));
if(sat == 1){
((TextView) view).setTextColor(Color.parseColor("#FFFFFF"));
}
return true;
}
return false;
}
});
lv = (ListView) v.findViewById(R.id.listView);
lv.setAdapter(adapter);
Perhaps I am going about this all wrong ?? It nearly works though.
Many thanks
Upvotes: 0
Views: 115
Reputation: 929
You still need to handle the cases where mon, tue, etc. == 0. Adding the following to each if statement should fix it:
else {
((TextView) view).setTextColor(//color that you want if day == 0);
}
Also, you may want to consider changing your whole setViewValue to use a switch statement, it should make the cod more readable.
Upvotes: 0