【发布时间】:2012-03-11 19:34:42
【问题描述】:
我正在为安卓开发一个闹钟应用程序,我想在主屏幕上显示闹钟列表。这个ListView 的每一行都在xml 文件中定义。我想在一周中的每一天都有单独的TextViews。例如,程序将检查 sqlite db。 monday 的值为 = 1,然后将此 TextView 的颜色更改为红色。我已经编写了这段代码,但这不起作用。怎么了?
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;
}
});
【问题讨论】:
标签: android sqlite simplecursoradapter android-cursor android-viewbinder