ActionBar Dropdown Spinner项默认为第一项

Tar*_*nfx 17 android android-3.0-honeycomb

我正在尝试设置默认情况下需要在微调器中选择的项的索引,但它始终默认为0(第1项)

actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

SpinnerAdapter spinnerAdapter =
            new ArrayAdapter<String>(activity, android.R.layout.simple_spinner_dropdown_item,
                    names);
int selectedIndex = actionBar.getSelectedNavigationIndex();
if (selectedIndex != targetIndex) {
    actionBar.setSelectedNavigationItem(targetIndex);
}
Run Code Online (Sandbox Code Playgroud)

如果总是调用块,则在上面.即使在设置索引2之后,下次我检查它也会返回0.

编辑:我怀疑getSelectedNavigationIndex给出了actionBar项的索引而不是Spinner下拉项.如果是这种情况,什么方法设置下拉列表中所选项目的索引?

Rom*_*man 34

确保在更改所选元素之前调用setListNavigationCallbacks方法.我在你的例子中看不到它,所以我认为这就是问题所在.

这是一个例子:

actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setListNavigationCallbacks(adapter, this);
actionBar.setSelectedNavigationItem(position);
Run Code Online (Sandbox Code Playgroud)

它在我的应用程序中工作没有任何问题.