androidandroid-listview

How to highlight selected item in ListView?


I know that android doesn't highlight anything in TouchMode. But I am doing something similar to the gmail application in which you select stuff from the left side and show details on the right side of the activity(wonder how Google did that).

So the story is I have to highlight what's been selected on the left side ListView. I've found some similar questions and the solutions are basically:

1.override the adapter's getView method and setBackground for selected position

2.setBackground of the view onItemClick and clear it for anther selection

But none of them worked for me due to a weird behaviour: As I click on one item and highlight it, the fifth item after it is highlighted as well, and so on so forth as I scroll down the list.

Any suggestions? THX!


Solution

  • Use listView.setChoiceMode(int choiceMode);

    Parameters

    choiceMode One of CHOICE_MODE_NONE, CHOICE_MODE_SINGLE, or CHOICE_MODE_MULTIPLE from class android.widget.AbsListView

    http://developer.android.com/reference/android/widget/AbsListView.html#setChoiceMode(int)

    You also need to add MultiChoiceModeListener, you can have CHOICE_MODE_SINGLE

    (android.widget.AbsListView.MultiChoiceModeListener)

    Refer to the sample below

    http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List16.html