使用SimpleCursorAdapter.ViewBinder更改TextView的颜色

时间:2012-03-11 19:34:43

标签: android sqlite simplecursoradapter android-cursor android-viewbinder

我正在为Android开发一个闹钟应用程序,我希望在主屏幕上显示警报列表。此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;
        }
    });

2 个答案:

答案 0 :(得分:7)

来自SimpleCursorAdapter.ViewBinder上的Android文档:

  

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

换句话说,setViewValue的实施不应该针对任何一个View,因为SimpleCursorAdapter会对每个View进行更改(根据您的实施情况而定)当它填充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(用于指定一周中的特定日期)。

答案 1 :(得分:2)

来自SimpleCursorAdapter.ViewBinder上的Android文档:

  

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

换句话说,setViewValue的实施不应该针对任何一个View,因为SimpleCursorAdapter会对每个View进行更改(根据您的实施情况而定)当它填充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(用于指定一周中的特定日期)。