Search code examples
androidtextviewsimplecursoradapter

adding colors to simple cursor adapter list view


I was wondering what would be the best way to add color to each entries on the list that was created with simplecursoradapter? The following codes created a listview, using SimpleCursorAdapter, but the color of the text are all default grey. What I would like to do is to change the color of the text in R.id.rowTitle, and R.id.rowName to different colors depending on the date.

    Cursor cursor = database.getTitles();
    startManagingCursor(cursor);

    String[] columns = { SQLController.TITLE,
    SQLController.COLUMN_NAME, SQLController.DATE,
    };

    int to[] = {R.id.rowTitle, R.id.rowName,R.id.rowDate};

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
            R.layout.rows, cursor, columns, to);

    this.setListAdapter(adapter);

Solution

  • If you just wanted the different items to be different colors, you could just do a custom row layout.

    However, since you want to set it based on the content of the data, you will need to create a custom adapter (extend Simpl eCursorAdapter) and put an if statement in there to check the date and set your color for each textview using textview.setTextColor(color_ref);

    Tutorial