Android ListView setSelection() does not seem to work

前端 未结 17 1589
情歌与酒
情歌与酒 2020-11-28 06:45

I have a ListActivity that implements onListItemClick() and calls a doSomething() function of the class. The latter contains l.s

相关标签:
17条回答
  • 2020-11-28 07:01

    For me it helped to set
    ListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); or ListView.CHOICE_MODE_MULTIPLE
    then
    ListView.setSelection(position) or ListView.setItemChecked(position, true);
    works fine

    0 讨论(0)
  • 2020-11-28 07:02

    use requestFocusFromTouch() before calling setSelection() method

    0 讨论(0)
  • 2020-11-28 07:10

    maybe you need to use function:

    ListView.setItemChecked(int position, boolean checked);
    
    0 讨论(0)
  • 2020-11-28 07:13

    Simply try this code

      listView.setAdapter(adapter);
      listView.setSelection(position);
      adapter.notifyDataSetChanged(); 
    
    0 讨论(0)
  • 2020-11-28 07:14

    setSelection() does not necessarily have visual impact. The selection bar only appears if you use the D-pad/trackball to navigate the list. If you tap on the screen to click something, the selection bar appears briefly and vanishes.

    Hence, setSelection() will only have a visual impact if the activity is not in touch mode (i.e., the last thing the user did was use the D-pad/trackball).

    I am not 100% certain this explains your phenomenon given the description you provided, but I figured it is worth a shot...

    0 讨论(0)
提交回复
热议问题