setItemChecked (int position, boolean value) not working?
Asked Answered
F

3

7

I have a listview which is customized to display an image and 2 textview. I just simply wanted to highlight one of the item from my list.

Firstly, I go with setSelection method of listview which i finally found out it is not the way as it is not working in touch mode.

So, I do some searching and found that I'd need to use setItemChecked method. Thus, I make a state-list color.

<?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 background color of my customized list item.

From List activity, I call setItemChecked(position,true) to a specific index of my listview.

Unfortunately, it doesn't seem to work for me. Is there anything missing? Anyone got luck with it?

Note**, I did retrieve data for list view from network. I do setItemChecked only after i have data in my listview.
My listview is in single choice mode too.

Fabri answered 12/9, 2011 at 7:19 Comment(3)
Show you are implementing the setItemCheck event.Macronucleus
listview.setItemChecked(1, true); Is that what you need? The call is in in onResume(). I did tried with many positions (0,1,2,3,...)Fabri
I am just curious. What does <item android:drawable="@color/WHITE" /> do?Trichomoniasis
U
8

I'm afraid that it is no easy way to do that in the Android Framework.

In order to get the setSelection(...) working, your View has to implement the follogin interface: android.widget.Checkable

You probably are using a some layout for View (an image and 2 textview in a LinearLayout maybe?), which doesn't implement the Checkable interface.

What you can do, is to create a custom View class which implements Checkable.

Check out the link below for a checkable LinearLayout:

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


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

@Override
public void setChecked(boolean checked) {
    if (checked) {
        this.setBackgroundColor(Color.RED);
    } else {
        setBackgroundColor(Color.BLACK);
    }
}
Unterwalden answered 12/9, 2011 at 12:54 Comment(2)
Let me know if it helped/worked, or you need more explanation.Unterwalden
Thz for the input. Unfortunately, this isn't what I am asking for. setSelection do work perfectly fine in your referred link. But it lacks of visual feedback.In other word, my list item didn't get highlighted although it is position correctly(listview gets scrolled to the selection). FYI, i did tested by following the link in your answer. :)Fabri
P
8

set for your list row background selector, which has 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>
Pincince answered 21/1, 2014 at 15:47 Comment(1)
This is the easiest way to highlight selected row in listview. It won't work if listview doesn't have attribute choiceMode android:choiceMode="singleChoice"Instigate
O
1

try including the android:state_enabled attribute as well.

Omen answered 12/9, 2011 at 7:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.