I'm trying to create a listview generated from information out of my SQLite DB. I currently have the code set up to just display each row accordingly and populate 3 textviews with data. I would like to be able to use some sort of If statement to check to see if one of values is "Room1" and then set the background of that row to a certain color.
This way each row in the list will have a different background based on what "room" the data is for.
I have tried extending SimpleCursorAdapter but I am a bit lost on how to get what I want out of it.
Here is what I have currently for the SimpleCursorAdapter:
Cursor notesCursor = myDbHelper.fetchDayPanelNotes(type, day);
startManagingCursor(notesCursor);
String[] from = new String[]{DataBaseHelper.KEY_NAME, DataBaseHelper.KEY_ROOM, DataBaseHelper.KEY_TIME};
int[] to = new int[]{R.id.text1, R.id.text2, R.id.text3};
SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.main_row, notesCursor, from, to);
setListAdapter(notes);
I would recommend extending BaseAdapter
, populating your list data manually and keeping a local copy of that data in your adapter class. Then when you implement the getView(...)
method you can use the position or ID of the View
you're generating to fetch the corresponding data to check against "Room1," and alter the View
you return based on that comparison.
The answer by rekaszeru has exactly the right idea, but it sounds like you'll need more information than the position and ID, which is why I recommend going a level lower and implementing BaseAdapter
directly.