I'm developing an alarm clock app for android and I want to have displayed list of alarms on the main screen. Each row of this ListView
is defined in xml file. And I want to have separate TextViews
for each day of week. Program will check in sqlite db if for eg. value for monday
is = 1 and then change color of this TextView
to red. I have written this code, but that doesn't work. What's wrong?
private void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = db.fetchAllAlarms();
startManagingCursor(c);
String[] from = new String[] { db.KEY_TIME, db.KEY_NAME };
int[] to = new int[] { R.id.time, R.id.alarmName };
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter alarms =
new SimpleCursorAdapter(this, R.layout.alarm_row, c, from, to);
alarms.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int dayOfWeekIndex = cursor.getColumnIndex("mon");
if (dayOfWeekIndex == columnIndex) {
int color = cursor.getInt(dayOfWeekIndex);
switch(color) {
case 0: ((TextView) view).setTextColor(Color.RED); break;
case 1: ((TextView) view).setTextColor(Color.GRAY); break;
}
return true;
}
return false;
}
});
From the Android documentation on SimpleCursorAdapter.ViewBinder
:
Binds the Cursor column defined by the specified index to the specified view. When binding is handled by this ViewBinder, this method must return true. If this method returns false, SimpleCursorAdapter will attempts to handle the binding on its own.
In other words, your implementation of setViewValue
should not be specific to any one View
, as SimpleCursorAdapter
will make changes to each View
(according to your implementation) when it populates the ListView
. setViewValue
is basically your chance to do whatever you wish with the data in your Cursor
, including setting the color of your views. Try something like this,
public boolean setViewValue(View view, Cursor cursor, int columnIndex){
// if this holds true, then you know that you are currently binding text to
// the TextView with id "R.id.alarmName"
if (view.getId() == R.id.alarmName) {
final int dayOfWeekIndex = cursor.getColumnIndex("day_of_week");
final int color = cursor.getInt(dayOfWeekIndex);
switch(color) {
case 0: ((TextView) view).setTextColor(Color.RED); break;
case 1: /* ... */ break;
case 2: /* ... */ break;
/* etc. */
}
return true;
}
return false;
}
Note that the above code assumes a column named "day_of_week"
which holds an int
value 0-6 (to specify the specific day of the week).
From the Android documentation on SimpleCursorAdapter.ViewBinder
:
Binds the Cursor column defined by the specified index to the specified view. When binding is handled by this ViewBinder, this method must return true. If this method returns false, SimpleCursorAdapter will attempts to handle the binding on its own.
In other words, your implementation of setViewValue
should not be specific to any one View
, as SimpleCursorAdapter
will make changes to each View
(according to your implementation) when it populates the ListView
. Your implementation should look something like this,
notes.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int dayOfWeekIndex = cursor.getColumnIndex("day_of_week");
if (dayOfWeekIndex == columnIndex) {
int color = cursor.getInt(dayOfWeekIndex);
switch(color) {
case 0: ((TextView) view).setTextColor(Color.RED); break;
case 1: /* ... */ break;
case 2: /* ... */ break;
/* etc. */
}
return true;
}
return false;
}
});
Note that the above code assumes a column named "day_of_week"
which holds an int
value 0-6 (to specify the specific day of the week).
来源:https://stackoverflow.com/questions/9658201/using-simplecursoradapter-viewbinder-to-change-the-color-of-textview