Note that there are some explanatory texts on larger screens.

plurals
  1. POChange background color based on property
    text
    copied!<p>I want to cheange the color of the background of a ListItem based on some property of the current member of the cursor. I'm using SimpleCursorAdapter to bind the values of the database to the ListView. What I did is to create a color selector like this:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_activated="true" android:drawable="@android:color/holo_blue_dark" /&gt; &lt;item android:state_activated="true" android:state_selected="true" android:drawable="@android:color/holo_blue_dark" /&gt; &lt;item android:state_selected="true" android:drawable="@color/rosa" /&gt; &lt;item android:drawable="@android:color/transparent" /&gt; &lt;/selector&gt; </code></pre> <p>My layout is:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="@color/listitem_color" &gt; ... ... </code></pre> <p>And the I have a view binder where I do the following:</p> <pre><code>mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.listitem, cur, cols, to,0); mAdapter.setViewBinder(new ViewBinder() { @Override public boolean setViewValue(View view, Cursor cursor, int columnIndex) { if (columnIndex == 6) { boolean dudoso = cursor.getInt(columnIndex) == 1; TextView textView = (TextView) view; textView.setText(dudoso ? "dudoso" : null); if(dudoso) { LinearLayout ll=((LinearLayout)textView.getParent().getParent()); ll.setSelected(true); } return true; } return false; } }); </code></pre> <p>But it's not working. What is the correct way to do what I want?</p>
 

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