Android ListView setSelection() does not seem to work

I have a ListActivity that implements onListItemClick() and calls a doSomething() function of the class. The latter contains l.setSelection(position) where l is the ListView object.

Now there is a onClickListener() listening for a button click that perfoms some actions and that too calls doSomething().

In the first case, the selected item get positioned appropriately, but in the latter, nothing happens.

Any clues about this strange behaviour and how I might make it work?


Solution 1:

maybe you need to use function:

ListView.setItemChecked(int position, boolean checked);

Solution 2:

use requestFocusFromTouch() before calling setSelection() method