Note that there are some explanatory texts on larger screens.

plurals
  1. POChange the background color of only selected entries in a ListView
    primarykey
    data
    text
    <p>I have a ListActivity in which I show some entries. Once an entry is selected, I show another Activity with the content of the entry. I'd like, when I return back to the ListActivity, that the unread entries had a different color than the read ones. </p> <p>I have a DataBase that stores the entries and when I select one the DataBase field COLUMN_READ is updated.</p> <p>I have a custom ListAdapter:</p> <pre><code> public class CustomListAdapter extends SimpleCursorAdapter{ private Context context; private int layout; public CustomListAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { super(context, layout, c, from, to); this.context = context; this.layout = layout; } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { Cursor c = getCursor(); final LayoutInflater inflater = LayoutInflater.from(context); View v = inflater.inflate(layout, parent, false); int titleCol = c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_TITLE); int authorCol=c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_AUTHOR_NAME); int readCol= c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_READ); String title = c.getString(titleCol); String author = c.getString(authorCol); long read= c.getLong(readCol); TextView name_text = (TextView) v.findViewById(R.id.title); if (name_text != null) { name_text.setText(title); } TextView content_text = (TextView) v.findViewById(R.id.subtitle); if (content_text != null) { content_text.setText(author); } if (read!=0){ name_text.setBackgroundColor(Color.GRAY); } return v; } @Override public void bindView(View v, Context context, Cursor c) { int titleCol = c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_TITLE); int authorCol=c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_AUTHOR_NAME); int readCol= c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_READ); String title = c.getString(titleCol); String author = c.getString(authorCol); long read= c.getLong(readCol); TextView name_text = (TextView) v.findViewById(R.id.title); if (name_text != null) { name_text.setText(title); } TextView content_text = (TextView) v.findViewById(R.id.subtitle); if (content_text != null) { content_text.setText(author); } if (read!=0){ name_text.setBackgroundColor(Color.GRAY); } } } </code></pre> <p>And what I do is:</p> <pre><code> CustomListAdapter present = new CustomListAdapter(context, R.layout.atom_list_row, loadingCursor, new String[] {DataBaseSchema.EntrySchema.COLUMN_TITLE,DataBaseSchema.EntrySchema.COLUMN_AUTHOR_NAME, DataBaseSchema.EntrySchema.COLUMN_READ}, new int[]{R.id.title,R.id.subtitle,R.id.row_container}); setListAdapter(present); </code></pre> <p>The problem is that the highlighted entries are not correctly shown, and I don't understand why. The background color in fact changes for the selected entry, but also for some other, it seems randomly, and also when I scroll other entries change their color. Is it an Android bug? Are there some workaround, or I am missing something?</p> <p>Thanks in advance..</p> <p>EDIT I've found a solution!!!! I've added <code>v.refreshDrawableState();</code> at the end of my code. This code works:</p> <pre><code> public class CustomListAdapter extends SimpleCursorAdapter{ private Context context; private int layout; public CustomListAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { super(context, layout, c, from, to); this.context = context; this.layout = layout; } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { Cursor c = getCursor(); final LayoutInflater inflater = LayoutInflater.from(context); View v = inflater.inflate(layout, parent, false); int titleCol = c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_TITLE); int authorCol=c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_AUTHOR_NAME); int readCol= c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_READ); String title = c.getString(titleCol); String author = c.getString(authorCol); long read= c.getLong(readCol); TextView name_text = (TextView) v.findViewById(R.id.title); if (name_text != null) { name_text.setText(title); } TextView content_text = (TextView) v.findViewById(R.id.subtitle); if (content_text != null) { content_text.setText(author); } if (read!=0){ v.setBackgroundColor(Color.GRAY); } else v.setBackgroundColor(Color.BLACK); v.refreshDrawableState(); return v; } @Override public void bindView(View v, Context context, Cursor c) { int titleCol = c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_TITLE); int authorCol=c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_AUTHOR_NAME); int readCol= c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_READ); String title = c.getString(titleCol); String author = c.getString(authorCol); //long read= c.getLong(readCol); TextView name_text = (TextView) v.findViewById(R.id.title); if (name_text != null) { name_text.setText(title); } TextView content_text = (TextView) v.findViewById(R.id.subtitle); if (content_text != null) { content_text.setText(author); } //LinearLayout linear =(LinearLayout) v.findViewById(R.id.row_container); if (c.getLong(readCol)!=0){ v.setBackgroundColor(Color.GRAY); }else v.setBackgroundColor(Color.BLACK); v.refreshDrawableState(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload