8

I have a simple cursor adapter set on a list in my application as follows:

private static final String fields[] = {"GenreLabel", "Colour", BaseColumns._ID};


datasource = new SimpleCursorAdapter(this, R.layout.row, data, fields, new int[]{R.id.genreBox, R.id.colourBox});

R.layout.row consists of two TextViews (genreBox and colourBox). Rather than setting the content of the TextView to the value of "Colour" , I would like to set its background colour to that value.

What would I need to do to achieve this?

Hamid
  • 4,410
  • 10
  • 43
  • 72

2 Answers2

13

Check out SimpleCursorAdapter.ViewBinder.

setViewValue is basically your chance to do whatever you wish with the data in your Cursor, including setting the background color of your views.

For example, something like:

SimpleCursorAdapter.ViewBinder binder = new SimpleCursorAdapter.ViewBinder() {
    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        String name = cursor.getColumnName(columnIndex);
        if ("Colour".equals(name)) {
            int color = cursor.getInt(columnIndex);
            view.setBackgroundColor(color);
            return true;
        }
        return false;
    }
}
datasource.setViewBinder(binder);

Update - if you're using a custom adapter (extending CursorAdaptor) then the code doesn't change a whole lot. You'd be overriding getView and bindView:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView != null) {
        return convertView;
    }
    /* context is the outer activity or a context saved in the constructor */
    return LayoutInflater.from(context).inflate(R.id.my_row);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    int color = cursor.getInt(cursor.getColumnIndex("Colour"));
    view.setBackgroundColor(color);
    String label = cursor.getString(cursor.getColumnIndex("GenreLabel"));
    TextView text = (TextView) findViewById(R.id.genre_label);
    text.setText(label);
}

You're doing a bit more manually, but it's more or less the same idea. Note that in all of these examples you might save on performance by caching the column indices instead of looking them up via strings.

Matthew
  • 44,826
  • 10
  • 98
  • 87
  • Thank you, this did exactly what I needed and very simply too. – Hamid Apr 06 '11 at 23:12
  • 1
    Great! I have always used custom adapters, but often what you need can be done simply with ViewBinder. – Matthew Apr 07 '11 at 05:25
  • Is there any chance you could also provide an example of the custom adapter method of doing this? Eventually I will have a whole load of adapters in my app and i think extending the simplecursoradapter might be the best way in the long run. – Hamid Apr 07 '11 at 09:10
  • Added some code. Note that you probably want to extend `CursorAdapter` and not `SimpleCursorAdapter`. – Matthew Apr 07 '11 at 14:32
  • Thanks, the second method looks more scalable to what I want to do eventually. And thanks for the note about CursorAdapter vs SimpleCursorAdapter. – Hamid Apr 07 '11 at 15:17
0

What you're looking for requires a custom cursor adapter. You can subclass SimpleCursorAdapter. This basically give access to the view as its created (although you'll be creating it yourself).

See this blog post on custom CursorAdapters for a complete example. Particularly, I think you'll need to override bindView.

skabbes
  • 890
  • 7
  • 17