I have a listview that when I select an item, it populates a details view. I want to show the item that was selected in the listview by changing it to green. Trouble is, the selected item does not show as green until I’ve clicked the same item two times. What am I doing wrong on
the selected item that keeps it from changing to green until the item is
clicked two times? Here is my xml selector file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Active list item -->
<item android:state_selected="true"
android:state_focused="false"
android:state_pressed="false"
android:drawable="@drawable/lv_bg_selected" />
<!-- Inactive list item -->
<item android:state_selected="false"
android:state_focused="false"
android:state_pressed="false"
android:drawable="@drawable/lv_bg_unselected_state" />
<!-- Pressed list item -->
<item android:state_pressed="true"
android:drawable="@drawable/lv_bg_pressed_state" />
</selector>
And onListitemClick:
@Override
public void onListItemClick(ListView l, View v, int position, long id)
{
.
.
.
v.setSelected(true);
.
.
.
}
Add the default state to the selector xml, specifically as the last item.
Update:
Use the android:state_activated in your selector,
setChoiceMode(ListView.CHOICE_MODE_SINGLE)on your listview and callsetItemChecked()when an item is clicked.Inflate
android.R.layout.simple_list_item_activated_1in your getView method to get theCHOICE_MODE_SINGLEworking.For more details, refer this post :
Showing the current selection in a listview