【问题标题】:Using SimpleCursorAdapter.ViewBinder to change the color of TextView使用 SimpleCursorAdapter.ViewBinder 改变 TextView 的颜色
【发布时间】: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


    【解决方案1】:

    来自SimpleCursorAdapter.ViewBinder 上的 Android 文档:

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

    换句话说,setViewValue 的实现不应特定于任何View,因为SimpleCursorAdapter 将在填充ListView 时更改每个View(根据您的实现)。 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(以指定一周中的特定日期)。

    【讨论】:

    • 老实说现在比以前更糟,因为它崩溃了。在开始时它执行 alarmNameIndex = cursorgetColumnIndex(db.KEY_NAME.toString());然后它变成1,然后columnIndex在2次迭代后变成1,然后突然columnIndex变成-1
    • 哦,对不起。这都是我的错。我没有将你的 "day_of_week_" 更改为我的 "mon"。但现在问题是一样的。在执行该函数期间 columnIndex 为 1 或 2,但 dayOfWeekIndex 为 3,所以它不起作用。
    • 顺便问一下,“mon”到底是什么……?如果星期一没有设置闹钟,它是否只是保持“0”,如果设置为星期一,它是否只是保持“0”?
    • 另外,我不知道您为什么认为“dayOfWeekIndex”等于 3 表示代码不起作用... dayOfWeekIndex 永远不会等于 columnIndex,因为您从未告诉过从“mon”列绑定的SimpleCursorAdapter
    • 是的,如果没有为星期一设置闹钟,则“mon”保持“0”,如果设置为星期一,则保持“1”。因此,如果星期一设置为星期一,我想更改星期一的 TextView 的颜色。主屏幕如下所示:img23.imageshack.us/img23/8468/snapshot1me.png
    【解决方案2】:

    来自SimpleCursorAdapter.ViewBinder 上的 Android 文档:

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

    换句话说,setViewValue 的实现不应特定于任何View,因为SimpleCursorAdapter 将在填充ListView 时更改每个View(根据您的实现)。你的实现应该是这样的,

    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(以指定一周中的特定日期)。

    【讨论】:

    • 非常感谢,这很有帮助,但我不想更改每个视图,我只想更改与特定日期关联的一个文本视图。第二件事是 columnIndex 的值为 1 和 2,但我的光标中有 10 列..
    • 我不是 100% 确定出了什么问题,但您应该重新考虑在代码中硬编码 int 值。例如,说if (columnIndex == 5) 不是一个好习惯,因为如果您将来更改数据库,这段代码将会中断。请参阅我的代码以获取有关如何更改此设置的示例(这也可能是您无法正常工作的原因...)
    • 是的,我已经按照你写的那样做了,所以我没有硬编码的 int 值,但 'if' 条件中的语句永远不会为真,因为它只检查前两列我不知道为什么。
    • 所以等一下,让我正确理解...您想更新 特定 Views 颜色 如果其中一个警报设置为一周中的特定日期?
    • 另外,我不明白TextViews 的显示位置。如果ListView正在主屏幕上显示,那么你说的这七个TextView在哪里?
    猜你喜欢
    • 2012-12-26
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 2016-08-05
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多