解决ListView的setSelection()无效的方案有:
1、
代码:
ListView.setItemChecked(int position, boolean checked);
解释:
Ensure that the listview is set to CHOICE_MODE_SINGLE in the layout XML or in the java via: myList.setChoiceMode(ListView.CHOICE_MODE_SINGLE); and the setItemChecked worked for me, it moved the selection indicator to the item I passed in (unselecting the previous one).
2、
代码:
mListView.clearFocus();
mListView.post(new Runnable() {
@Override
public void run() {
mListView.setSelection(index);
}
});
解释:
You might need to wrap setSelection() in a posted Runnable (reference).
3、
代码:
adapter.notifyDataSetChanged();
mListView.setAdapter(adapter);
mListView.setSelection(index);
解释:
One workaround that fix it without side effects is to set the list adapter again on the list before calling setSelection().
转自:http://m.blog.csdn.net/blog/santamail/38821763#