views:

75

answers:

1

Hi,

An activity has a Button and a ListView. Initially, only the Button is visible. When the button is pressed, the ListView is displayed. When displayed, is it possible for me to show one particular item as selected/focussed?

A use case could be that suppose it is a list of language settings and when the list opens, the currently selected language must be shown as highlighted.

If I know the index of the item, how to set it as focused on display?

-Kiki

A: 

In short, ListView::setSelection(int position) is what you need. However, depending on whether the device is in touch mode or not, it may or may not have visual effect (background highlighting). For more details, refer to http://stackoverflow.com/questions/2042278/android-listview-selection-problem

Sun Jian
Nope, setSelection(pos) does not solve my problem! The device is not in touch mode, but still i do not get the backgound highlighting. I am simply testing on an emulator. My list view is using a selector xml, hope that has no effect on this!
kiki
How do you know your device is not in touch mode? Maybe you can try to use the navigation key to highlight the button and use the center key to press the button. See if that makes a difference.
Sun Jian
I am simply working with the emulator now in Eclipse. And using my PC's keyboard to work with.
kiki
How did you press the button? Did you use mouse or only keyboard?
Sun Jian
keyBoard's ENTER key.
kiki
Try adding the two lines: listView.setFocusable(true);listview.setFocusableInTouchMode(true);
Sun Jian
Well, I accept this answer. But felt a more presentable solution is to use a CheckedTextViw as my list items. And the default selected item is shown as checked. More easy to understand!
kiki