0

Following scripts sets the color of (for example) ListItem position 1, but it also gives number 12 (11+1) a nice grey color. Is this some kind of bug in Android?

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    ListView.setSelection(arg2);
    arg1.setBackgroundColor(Color.LTGRAY);
    adapter.notifyDataSetChanged();
}

2 Answers2

1

ListView recycles (reuses) the views. So you need to associate the background color with the data, not the view! Then, in getView() you have the chance to correctly set the background color based on the data.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
0

@David Wasser is correct ... cell re-use is causing multiple listview rows to draw with gray background.

However, if you're trying to set your background based on SELECTION STATE, consider this technique:

// set single or multi-select on your list (CHOICE_MODE_SINGLE = single row selectable)
// do this in onCreate
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
.
.
.
// in your onItemClick, set the checked state of the item
// you DO NOT need to call notifyDataSetChanged
listView.setItemChecked(position, true);

And, set the background on your listview cell layout to the built-in or a custom selector

BUILT-IN:

android:background="?android:attr/activatedBackgroundIndicator"

CUSTOM:

android:background="@drawable/myListBackground"

drawable/myListBackground.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true" android:drawable="@color/lightgray" />
    <item android:drawable="@color/transparent" />
</selector>

the key is the state_activated entry, which will be used when the item is selected/checked. You can also specify colors for other states, the above example references colors from a colors.xml table.

For more details on this, please check out How does "?android:attr/activatedBackgroundIndicator" work?

Community
  • 1
  • 1
CSmith
  • 13,318
  • 3
  • 39
  • 42