3

我正在为android开发一个闹钟应用程序,我想在主屏幕上显示闹钟列表。每一行都ListView在 xml 文件中定义。我想为TextViews一周中的每一天分开。例如,程序将检查 sqlite db。is = 1的值monday,然后将其颜色更改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;
        }
    });
4

2 回答 2

7

来自 Android 文档SimpleCursorAdapter.ViewBinder

将指定索引定义的 Cursor 列绑定到指定视图。当此 ViewBinder 处理绑定时,此方法必须返回 true。如果此方法返回 false,SimpleCursorAdapter 将尝试自行处理绑定。

换句话说,您的实现setViewValue不应该特定于任何一个View,因为SimpleCursorAdapterView填充ListView. setViewValue基本上你有机会对 中的数据做任何你想做的事情Cursor,包括设置视图的颜色。试试这样的,

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;
}

请注意,上面的代码假定一个名为的列"day_of_week"包含一个int值 0-6(以指定一周中的特定日期)。

于 2012-03-12T19:02:14.527 回答
2

来自 Android 文档SimpleCursorAdapter.ViewBinder

将指定索引定义的 Cursor 列绑定到指定视图。当此 ViewBinder 处理绑定时,此方法必须返回 true。如果此方法返回 false,SimpleCursorAdapter 将尝试自行处理绑定。

换句话说,您的实现setViewValue不应该特定于任何一个View,因为SimpleCursorAdapterView填充ListView. 你的实现应该是这样的,

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;
    }
});

请注意,上面的代码假定一个名为的列"day_of_week"包含一个int值 0-6(以指定一周中的特定日期)。

于 2012-03-12T01:42:06.093 回答