Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set activated item in ListView programmatically

I have a simple ListView with some items that have setChoiceMode set to ListView.CHOICE_MODE_SINGLE, which means when I touch an item, it is highlighted. This way the user can see what menu choice (the ListView is a menu) was chosen:

listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

However, I want to change that selection from code (not by touching). I tried this:

listView.setSelection(0);

but it seems to have no effect. Probably because selection and activation are different concepts. There is no setActivated(int) method available.

like image 323
Bart Friederichs Avatar asked Jan 21 '14 08:01

Bart Friederichs


2 Answers

you can select item with following code:

listView.setItemChecked(position, true);
like image 129
Shayan Pourvatan Avatar answered Nov 15 '22 19:11

Shayan Pourvatan


Using this method worked for me

listView.performItemClick(listView, position, listView.getItemIdAtPosition(position));
like image 29
karan vs Avatar answered Nov 15 '22 19:11

karan vs