Using SimpleCursorAdapter.ViewBinder to change color of TextView

I am developing an alarm clock app for android and I want to display a list of alarms on my home screen. Each line of this ListView

is defined in the xml file. And I want to have a separate one TextViews

for each day of the week. The program will check the sqlite db if, for example. value for monday

= 1 and then change the color of that TextView

to red. I wrote this code but it 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;
        }
    });

      

+3


source to share


2 answers


From Android documentation on SimpleCursorAdapter.ViewBinder

:

Binds the cursor column at the specified index to the specified view. When the binding is processed by this ViewBinder, this method should return true. If this method returns false, SimpleCursorAdapter will try to handle the binding on its own.

In other words, your implementation setViewValue

doesn't have to be specific to anyone View

, as it SimpleCursorAdapter

will make changes to each View

(according to your implementation) when it populates ListView

. setViewValue

is basically your chance to do whatever you want with the data in yours Cursor

, including customizing 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 named column "day_of_week"

that contains the value int

0-6 (to indicate a specific day of the week).

+7


source


From Android documentation on SimpleCursorAdapter.ViewBinder

:

Binds the cursor column at the specified index to the specified view. When the binding is processed by this ViewBinder, this method should return true. If this method returns false, SimpleCursorAdapter will try to handle the binding on its own.

In other words, your implementation setViewValue

doesn't have to be specific to anyone View

, as it SimpleCursorAdapter

will make changes to each View

(according to your implementation) when it populates ListView

. Your implementation should look something like this:



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

      

Note that the above code assumes a named column "day_of_week"

that contains the value int

0-6 (to indicate a specific day of the week).

+2


source







All Articles