Change background colour of ListView item based on SimpleCursorAdapter field?
I have a custom ListView item using RelativeLayout which looks like this...
| Time | Title |
| | Description |
NOTE: The lefthand 'Time' is a single full height TextView with 'Title' and 'Description being individual 'stacked' TextViews.
The ListView items represent TV programmes extracted from a SQLite DB with a SimpleCursorAdapter. This works开发者_开发知识库 fine but the next step is to indicate when a programme has been scheduled to record (the app is an Android client for a PC-based PVR application).
The SQL query selects 'start_time,title,description,is_set_to_record' the first three being text fields to bind to the TextViews and the last being boolean.
It seems I probably need to extend SimpleCursorAdapter so if 'is_set_to_record' is true then I set the background colour of all three TextViews to a different colour. The problem is that I don't know where I should be looking to do this.
Is this possible? If so, any pointers to where I should look would be appreciated.
In your extended SimpleCursorAdapter you can do something like this.:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
Cursor c = getCursor();
c.moveToPosition(position);
int col = c.getColumnIndex(is_set_to_record);
boolean isSet = c.getInt(col) == 1;
if (isSet) {
// Set the background color of the text.
} else {
// Set the background color to something else.
}
return v;
}
精彩评论