使用SimpleCursorAdapter.ViewBinder改变的TextView的颜色 [英] Using SimpleCursorAdapter.ViewBinder to change the color of TextView

查看:149
本文介绍了使用SimpleCursorAdapter.ViewBinder改变的TextView的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Android开发一个闹钟应用程序,我想已经显示报警的列表主屏幕上。这个的ListView 的每一行是XML文件中定义。我想有单独的 TextViews 为一周的每一天。程序将检查在sqlite的分贝如果如。值周一为= 1,然后改变这种的TextView 的颜色为红色。我写了这个code,但不起作用。怎么了?

 私人无效fillData(){

    //获取所有的音符从数据库和创建项目列表
    光标C = db.fetchAllAlarms();
    startManagingCursor(C);

    的String []从=新的String [] {db.KEY_TIME,db.KEY_NAME};
    INT []到=新INT [] {R.id.time,R.id.alarmName};

    //现在创建一个数组适配器,并将其设置为显示用我们的行
    SimpleCursorAdapter报警=
        新SimpleCursorAdapter(这一点,R.layout.alarm_row,C,从,到);
        alarms.setViewBinder(新SimpleCursorAdapter.ViewBinder(){
        公共布尔setViewValue(查看视图,光标指针,整数参数:columnIndex){
            INT dayOfWeekIndex = cursor.getColumnIndex(问);
            如果(dayOfWeekIndex ==参数:columnIndex){
                INT颜色= cursor.getInt(dayOfWeekIndex);
                开关(彩色){
                案件0:((TextView中)查看).setTextColor(Color.RED);打破;
                案例1:((TextView中)查看).setTextColor(Color.GRAY);打破;
                }
                返回true;
            }
            返回false;
        }
    });
 

解决方案

从Android文档的 SimpleCursorAdapter.ViewBinder

  

通过绑定指定索引的定义光标列   指定的视图。当绑定由该ViewBinder处理,这   方法必须返回true。如果此方法返回false,   SimpleCursorAdapter会尝试自己处理的结合。

换句话说,你的执行 setViewValue 不应该是具体到任何一个查看,因为 SimpleCursorAdapter 会当它填充的ListView <修改每个查看(根据您的实现)/ code>。 setViewValue 基本上是你的机会,做任何你想要的数据在你的光标,包括设置你的视图的颜色。尝试这样的事情,

 公共布尔setViewValue(查看视图,光标指针,整数参数:columnIndex){
    //如果成立,那么你知道你正在结合文本
    // id为TextView的R.id.alarmName
    如果(view.getId()== R.id.alarmName){
        最终诠释dayOfWeekIndex = cursor.getColumnIndex(DAY_OF_WEEK);
        最终诠释色= cursor.getInt(dayOfWeekIndex);

        开关(彩色){
        案件0:((TextView中)查看).setTextColor(Color.RED);打破;
        案例1:/ * ...... * /休息;
        案例2:/ * ...... * /休息;
        /* 等等。 */
        }
        返回true;
    }
    返回false;
}
 

请注意,上面的code假设一个名为列DAY_OF_WEEK其持有 INT 0值-6(用于指定一周中的特定一天)。

I'm developing an alarm clock app for android and I want to have displayed list of alarms on the main screen. Each row of this ListView is defined in xml file. And I want to have separate TextViews for each day of week. Program will check in sqlite db if for eg. value for monday is = 1 and then change color of this TextView to red. I have written this code, but that doesn't work. What's wrong?

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

解决方案

From the Android documentation on SimpleCursorAdapter.ViewBinder:

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.

In other words, your implementation of setViewValue should not be specific to any one View, as SimpleCursorAdapter will make changes to each View (according to your implementation) when it populates the ListView. setViewValue is basically your chance to do whatever you wish with the data in your Cursor, including setting the color of your views. Try something like this,

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

Note that the above code assumes a column named "day_of_week" which holds an int value 0-6 (to specify the specific day of the week).

这篇关于使用SimpleCursorAdapter.ViewBinder改变的TextView的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆