ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 使用SimpleCursorAdapter.ViewBinder更改TextView的颜色

2019-07-23 12:23:50  阅读:296  来源: 互联网

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


我正在为Android开发一个闹钟应用程序,我希望在主屏幕上显示警报列表.此ListView的每一行都在xml文件中定义.我希望每周的每一天都有单独的TextView.程序将检查sqlite db是否为例如.星期一的值= 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;
        }
    });

解决方法:

从SimpleCursorAdapter.ViewBinder上的Android文档:

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.

换句话说,setViewValue的实现不应该特定于任何一个View,因为SimpleCursorAdapter会在填充ListView时对每个View(根据您的实现)进行更改. setViewValue基本上是你用光标中的数据做任何你想做的事情的机会,包括设置你的视图的颜色.尝试这样的事情,

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”的列,其中包含0-6的int值(用于指定一周中的特定日期).

标签:android,sqlite,simplecursoradapter,android-cursor,android-viewbinder
来源: https://codeday.me/bug/20190723/1513564.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有