2

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);
 .
 .
 .
}
Marcin S.
  • 11,161
  • 6
  • 50
  • 63

1 Answers1

7

Add the default state to the selector xml, specifically as the last item.

<selector ...>
   ...
   ...
    <!-- Normal list item --> 
    <item android:drawable="@drawable/lv_bg_normal_state" />
</selector>

Update:

Use the android:state_activated in your selector, setChoiceMode(ListView.CHOICE_MODE_SINGLE) on your listview and call setItemChecked() when an item is clicked.

Inflate android.R.layout.simple_list_item_activated_1 in your getView method to get the CHOICE_MODE_SINGLE working.

For more details, refer this post : Showing the current selection in a listview

Community
  • 1
  • 1
Ron
  • 24,175
  • 8
  • 56
  • 97
  • 'Thanks for the reply. It works well right now but still has some issue. Whenever I click something on the details view, the selected item from the list view becomes unselected. It looks like it looses the focus and becomes unselected. How can I keep selected item green until I click on another item? regardless whether I check something in the details view or not. Thanks in advance for the answers.' – Marcin S. Jul 20 '12 at 19:32
  • Use the `android:state_activated` in your selector, setChoiceMode(ListView.CHOICE_MODE_SINGLE) on your listview and call setItemChecked() when an item is clicked. – Ron Jul 21 '12 at 08:34
  • 'Hi, thanks again for the reply however when I use android:state_activated="true" it says that it cannot find the resources. I cannot also use ListView.CHOICE_MODE_SINGLE and setItemChecked(). I think this is becasue I'm using a custom ArrayAdapter? If yes how to implement it to be able to inherit attributes from default list view? The ArrayAdapter constructor looks like: public MyArrayAdapter(Activity context, String[] names) { super(context, R.layout.lv_row_layout, android.R.layout.simple_list_item_single_choice, names); this.context = context; this.names = names; } – Marcin S. Jul 21 '12 at 14:28
  • Why are you not able to use `ListView.CHOICE_MODE_SINGLE` and `setItemChecked()`? Using Custom ArrayAdapter should not stop you in anyway.. and instead of `android:state_activated` u can use `android:state_checked` – Ron Jul 21 '12 at 14:39
  • It's just mine conclusion that Custom ArrayAdapter might affect checked lists items. In the simple ArrayAdapter you normally pass as an arguments: Activity, android.R.layout.simple_list_item_activated_1 for example, and array of items. In my custom adapter I pass only two arguments: Activity and the array. May it be the reason of not showing checked/selected items? I tried your solution with basic adapter and it works well. CommonsWare says"Use a row layout with an activated background (android.R.layout.simple_list_item_activated_1)" but how can I pass it to my adapter if it has only 2 args? – Marcin S. Jul 21 '12 at 20:08
  • Add a third constructor that takes 3 arguments and pass it to super.. or simply inflate `android.R.layout.simple_list_item_activated_1` in your `getView` method.. – Ron Jul 22 '12 at 08:07