Does SetItemChecked (int position, boolean value) not working?

I have a list that is configured to display an image and 2 textviews. I just wanted to highlight one of the elements from my list.

Firstly, I use the setSelection listview method, which finally found that it is not, since it does not work in touch mode.

So, I searched a bit and found that I would need to use the setItemChecked method. Thus, I am doing the color of the state list.

<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@color/checkbox_bg_fcs" /> <item android:drawable="@color/WHITE" /> </selector> 

I used it to set the background color of my custom list item.

From the activity of the list, I call setItemChecked (position, true) to the specific index of my list.

Unfortunately, this does not work for me. Is something missing? Has anyone been lucky with him?

Note **, I extracted data to view the list from the network. I do setItemChecked only after I have the data in my list.
My list is also in single select mode.

+7
source share
3 answers

I am afraid this is an easy way to do this in the Android Framework.

To make setSelection (...) work, your View must implement the follogin interface: android.widget.Checkable

Perhaps you are using some kind of layout for the presentation (image and 2 text image in LinearLayout, maybe?), Which does not implement the Checkable interface.

What you can do is create a custom View class that implements Checkable.

See the link below for the tested LinearLayout:

http://tokudu.com/2010/android-checkable-linear-layout/


If you want to change the background, rewrite the setChecked method to do what you want. A very simple example:

 @Override public void setChecked(boolean checked) { if (checked) { this.setBackgroundColor(Color.RED); } else { setBackgroundColor(Color.BLACK); } } 
+8
source

set the background selector of the list row for your list that has the resource for state_activated:

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_activated="true" android:state_enabled="true" android:drawable="@android:color/black"></item> <item android:drawable="@android:color/transparent" android:state_enabled="true"/> </selector> 
+8
source

try to include the android:state_enabled .

+1
source

All Articles