ActionBar 下拉微调器项目默认为第一项

发布于 2024-12-20 23:18:43 字数 638 浏览 0 评论 0原文

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

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);
}

。即使设置索引 2 后,下次检查时它也会返回 0。

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

I'm trying to set the index of the item that needs to be selected in the spinner by default, but it always defaults to 0 (1st item)

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);
}

Above if block is called always. Even after setting index 2, next time I check it returns 0.

Edit: I suspect getSelectedNavigationIndex gives index of actionBar item rather than Spinner dropdown item. If that is the case, what method sets the index of selected item inside dropdown?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅紫色的梦幻 2024-12-27 23:18:43

确保您调用 setListNavigationCallbacks 方法。我在你的例子中看不到它,所以我认为这就是问题所在。

这是一个例子:

actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setListNavigationCallbacks(adapter, this);
actionBar.setSelectedNavigationItem(position);

它在我的应用程序中运行没有任何问题。

Make sure you call setListNavigationCallbacks method before changing selected element. I can't see it in your example, so I think that's the problem.

Here is an example:

actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setListNavigationCallbacks(adapter, this);
actionBar.setSelectedNavigationItem(position);

It works in my app without any problems.

╰◇生如夏花灿烂 2024-12-27 23:18:43

您是否尝试过使用共享首选项来保存所选微调器的值。我使用此代码来保存具有共享首选项的用户选择,以便下次他们打开应用程序时,微调器设置为他们选择的最后一个值:

Spinner Spinner = (Spinner) findViewById(R.id.Spinner);
String[] spinnervalues = getSpinnervalues();
ArrayAdapter<CharSequence> spinnerAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, makes);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner.setAdapter(spinnerAdapter);
int position = Utils.getIndex(getSpinnerval(), makes);

Spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        String selected = (String) parentView.getSelectedItem();
        Spinner Spinner = (Spinner) findViewById(R.id.Spinner);
        String[] spinnervalues = Filter.this.getSpinnerval(selected);
        ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(Filter.this, android.R.layout.simple_spinner_item, models);
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        Spinner.setAdapter(adapter);
    }

    public void onNothingSelected(AdapterView<?> parentView) {}
});

Spinner.setSelection(makeposition, true);
int position = Utils.getIndex(getSpinnerval());
if (position >= 0) {
    Spinner Spinner = (Spinner) findViewById(R.id.Spinner);
    Spinner.setSelection(position, true);
}

然后是 get Util:

public String getSpinnerval() {
    return getSharedPreferences().getString("val", "");
}

我稍微更改了代码,因此它可能不是 100% 正确但可能会给你一个想法。

Have you tried using shared prefences to save the value of the selected spinner. I used this code to save the users selection with shared preferences so the next time they opened the app the spinner was set to the last value they chose:

Spinner Spinner = (Spinner) findViewById(R.id.Spinner);
String[] spinnervalues = getSpinnervalues();
ArrayAdapter<CharSequence> spinnerAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, makes);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner.setAdapter(spinnerAdapter);
int position = Utils.getIndex(getSpinnerval(), makes);

Spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        String selected = (String) parentView.getSelectedItem();
        Spinner Spinner = (Spinner) findViewById(R.id.Spinner);
        String[] spinnervalues = Filter.this.getSpinnerval(selected);
        ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(Filter.this, android.R.layout.simple_spinner_item, models);
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        Spinner.setAdapter(adapter);
    }

    public void onNothingSelected(AdapterView<?> parentView) {}
});

Spinner.setSelection(makeposition, true);
int position = Utils.getIndex(getSpinnerval());
if (position >= 0) {
    Spinner Spinner = (Spinner) findViewById(R.id.Spinner);
    Spinner.setSelection(position, true);
}

Then the get Util:

public String getSpinnerval() {
    return getSharedPreferences().getString("val", "");
}

I altered the code a little so it might not be 100% right but may give you an idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文