Check out SimpleCursorAdapter.ViewBinder .
setViewValue is basically your chance to do whatever you want with the data in Cursor , including adjusting 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 use a custom adapter ( CursorAdaptor extension), then the code will not change completely. You would override getView and bindView :
@Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView != null) { return convertView; } 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 do a little more by hand, but this is more or less the same idea. Note that in all of these examples, you can save on performance by caching column indices rather than looking through them row by row.
Matthew willis
source share