Strange ListView selection behavior on Android

Received an action that extends ListActivity. The list is backed up by a custom adapter that extends BaseAdapter.

getListView().setFocusable(true);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

I am doing a simple check (getSelectedItemPosition() == ListView.INVALID_POSITION)in onPrepareOptionsMenu()to disable items for which you want to select an item.

Now, what I do after starting the activity (the action takes place under the emulator, ver.1.1), what it looks like:

  • Menu hit - menu items are disabled - OK
  • Select an item with the mouse wheel and click on the menu - the elements are activated -Good
  • Click anywhere outside the list, repeat 2). Items don't get enabled - WTF?
  • Start from scratch, select the element hitting Del and moving the mouse. the result is the same as for 3.

Why do these (3, 4) things continue to happen to me? :) TIA.

+3
source share
1 answer

Leaning on selectedItemPositiona ListViewcan be a dangerous approach. In my experience, if the List loses focus (by clicking something else), it selectedItemPositiongets the value INVALID_POSITION.

Basically, if your element does not have an orange color, it is highlighted, look at selectedItemPositionzero.

Alternatively, you may want to remember the selected item by overriding the methods onItemClickand onItemSelectionand store the index of the selected item, and then use this to control the accessibility of your menu.

+4
source

Source: https://habr.com/ru/post/1703574/


All Articles