0

I have a problem with "setItemChecked", I try select default item, with a background "listSelector". I think that the error is to "listSelector", but I don't know exactly

My listview :

<ListView
            android:id="@+id/list"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:listSelector = "@drawable/listitem_background"
            android:focusable="true"


            android:choiceMode="singleChoice"

            android:focusableInTouchMode="true"
            >
        </ListView>

My code select :

 this.listView.setItemChecked(1, true);

But not showing background selected.

When I use :

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {.... 

Works correctly, change color.

How I can fix this problem ?

Update 20/04/2015 10:07

XML selector

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

After instanciate adapter I try select ítem

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
this.listView.setSelection(2); // not work
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Calimbo
  • 81
  • 1
  • 3
  • 13

2 Answers2

0

Use setSelected() instead of setChecked()

pvn
  • 2,016
  • 19
  • 33
  • @Calimbo the setSelection() takes integer – pvn Apr 20 '15 at 07:57
  • setChecked() is meant for checkbox and radioBtns – pvn Apr 20 '15 at 07:57
  • I know "setSelection()" take number, but not work for me (setItemChecked ,setSelection()) not work ... is possible that the error is listSelector from XML ? – Calimbo Apr 20 '15 at 08:00
  • Can you describe what you want to achieve in a bit more detail. Do you want to allow multiple selections in a listview with selected items set to another color, or do you want to explicitly click an item in list programmatically – pvn Apr 20 '15 at 08:02
  • So you want the selected items to be shown with a different color. For that this approach won't work. You will have to create a custom adapter. Then you will have to store the position of selected items. In the getView() method of your adapter you will have to check whether the current position in getView() is present in the list of selected items if so set a different bkgrnd color. Whenever you update the selected items list call notifyDataSetChanged() – pvn Apr 20 '15 at 08:15
  • Correct but in my expandable I can do ExpandList.setItemChecked(childExpanHide, true); , in my listview I can't ??? not is possible.. – Calimbo Apr 20 '15 at 08:21
  • See this post http://stackoverflow.com/questions/21253067/set-activated-item-in-listview-programmatically – Calimbo Apr 20 '15 at 08:25
  • Expandable list view is meant to have an selected item, as the child items of the selected item will be expanded beneath it. In case of ListView you simply show a list of items and clicking it would perform an anction such as load a new page. In case you want to make it set as selected tab you will have to follow the approach I mentioned, or you can use expanded list view for the same thing and keep no child items. Implementing it using the approach I had mention is the normally followed. – pvn Apr 20 '15 at 11:28
  • I want a item selected default, but I can't check item 1 or 2 or 3...x, because my list not work (setItemChecked ,setSelection())) – Calimbo Apr 21 '15 at 09:57
0

You can Reference How does "?android:attr/activatedBackgroundIndicator" work? If you use a custom layout(like textview) for each row of listview: You can try this:

1、set listview android:choiceMode="singleChoice" 2、listView.setItemChecked(position, true) 3、apply it as the background of the custom layout(like textview)(not set to listview):

<?xml version="1.0" encoding="utf-8"?>

<item android:state_activated="true" android:drawable="@color/listview_selectitem_color" />
<item android:drawable="@android:color/transparent" />

Hope useful!

Community
  • 1
  • 1
  • I solved, 20/04/2015, the first problem is android.R.layout.simple_list_item_1, because he @override my functions with selectors, I created a adapter and Works correctly, I don't know correctly why. – Calimbo Apr 24 '15 at 07:38