Question

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);
Was it helpful?

Solution

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.

OTHER TIPS

You should override the getView method of your adapter:

SimpleCursorAdapter notes = new SimpleCursorAdapter(
        this, R.layout.main_row, notesCursor, from, to)
{
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        final View row = super.getView(position, convertView, parent);
        if (position % 2 == 0)
            row.setBackgroundResource(android.R.color.darker_gray);
        else
            row.setBackgroundResource(android.R.color.background_light);
        return row;
    }
};

and set the background of the row based on the position (is it odd or even).

This should do it. Thanks to this post, I had exact same problem but after going through this, problem solved as shown below,

    SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.notes_row, mNotesCursor, from, to)
    {
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            cursor.moveToPosition(position);
            String s = cursor.getString(mursor.getColumnIndex("roomnumber"));
            final View row = super.getView(position, convertView, parent);

            if (s.equalsIgnoreCase("room1"))
                row.setBackgroundColor(Color.parseColor("#480000"));
            else if (s.equalsIgnoreCase("room2"))
                row.setBackgroundColor(Color.parseColor("#000040"));
            else 
                row.setBackgroundColor(Color.parseColor("#004000"));
            return row;
        }
    };       


    setListAdapter(notes);

I implement SimpleCursorAdapter.ViewBinder on my Activity then override setViewValue...

public class MyActivity extends Activity
    implements SimpleCursorAdapter.ViewBinder {

    SimpleCursorAdapter myAdapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState)

        // Initialize myAdapter
        myAdapter.setViewBinder(this);
    }

    @Override
    public boolean setViewValue(View view, Cursor cursor, int column) {
        // Put code to process row data here
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top