Android 以编程方式设置微调器的选定项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11072576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:54:11  来源:igfitidea点击:

Set selected item of spinner programmatically

androidspinner

提问by Boardy

I am working on an android project and I am using a spinner which uses an array adapter which is populated from the database.

我正在开发一个 android 项目,我正在使用一个使用从数据库填充的数组适配器的微调器。

I can't find out how I can set the selected item programmatically from the list. For example if, in the spinner I have the following items:

我不知道如何从列表中以编程方式设置所选项目。例如,如果在微调器中我有以下项目:

  • Category 1
  • Category 2
  • Category 3
  • 类别 1
  • 类别 2
  • 类别 3

How would I programmatically make Category 2 the selected item when the screen is created. I was thinking it might be similar to c# I.E Spinner.SelectedText = "Category 2" but there doesn't seem to be any method similar to this for Android.

创建屏幕时,我将如何以编程方式使类别 2 成为所选项目。我在想它可能类似于 c# IE Spinner.SelectedText = "Category 2" 但似乎没有任何类似于 Android 的方法。

回答by Arun George

Use the following: spinnerObject.setSelection(INDEX_OF_CATEGORY2).

使用以下内容: spinnerObject.setSelection(INDEX_OF_CATEGORY2).

回答by Marco HC

No one of these answers gave me the solution, only worked with this:

这些答案都没有给我解决方案,只能解决这个问题:

mySpinner.post(new Runnable() {
    @Override
    public void run() {
        mySpinner.setSelection(position);
    }
});

回答by Yaqub Ahmad

public static void selectSpinnerItemByValue(Spinner spnr, long value) {
    SimpleCursorAdapter adapter = (SimpleCursorAdapter) spnr.getAdapter();
    for (int position = 0; position < adapter.getCount(); position++) {
        if(adapter.getItemId(position) == value) {
            spnr.setSelection(position);
            return;
        }
    }
}

You can use the above like:

您可以使用上述内容,例如:

selectSpinnerItemByValue(spinnerObject, desiredValue);

& of course you can also select by index directly like

&当然你也可以直接按索引选择

spinnerObject.setSelection(index);

回答by sberezin

Some explanation (at least for Fragments - never tried with pure Activity). Hope it helps someone to understand Android better.

一些解释(至少对于片段 - 从未尝试过纯活动)。希望它可以帮助人们更好地了解 Android。

Most popular answer by Arun Georgeis correct but don't work in some cases.
The answer by Marco HCuses Runnable wich is a last resort due to additional CPU load.

Arun George最受欢迎的答案是正确的,但在某些情况下不起作用。Marco HC
的答案使用 Runnable,这是由于额外的 CPU 负载而不得已的方法。

The answer is - you should simply choose correct place to call to setSelection(), for example it worksfor me:

答案是-你应该简单地选择正确的地方打电话到为setSelection() ,例如,它的工作对我来说:

@Override
public void onResume() {
    super.onResume();

    yourSpinner.setSelection(pos);
 }

But it won't work in onCreateView(). I suspect that is the reason for the interest to this topic.

但它不会在 onCreateView() 中工作。我怀疑这就是对这个话题感兴趣的原因。

The secret is that with Android you can't do anything you want in any method - oops:( - components may just not be ready. As another example - you can't scroll ScrollView neither in onCreateView() nor in onResume() (see the answer here)

秘诀是,在 Android 中,您无法在任何方法中做任何您想做的事情 - 哎呀:( - 组件可能还没有准备好。作为另一个例子 - 您不能在 onCreateView() 和 onResume() 中滚动 ScrollView (在这里看到答案)

回答by Ferran Maylinch

To find a value and select it:

要查找值并选择它:

private void selectValue(Spinner spinner, Object value) {
    for (int i = 0; i < spinner.getCount(); i++) {
        if (spinner.getItemAtPosition(i).equals(value)) {
            spinner.setSelection(i);
            break;
        }
    }
}

回答by RicardoSousaDev

Why don't you use your values from the DB and store them on an ArrayList and then just use:

为什么不使用数据库中的值并将它们存储在 ArrayList 上,然后使用:

yourSpinner.setSelection(yourArrayList.indexOf("Category 1"));

回答by Naham Al-Zuhairi

The optimal solution is:

最优解是:

    public String[] items= new String[]{"item1","item2","item3"};
    // here you can use array or list 
    ArrayAdapter adapter= new ArrayAdapter(Your_Context, R.layout.support_simple_spinner_dropdown_item, items);
    final Spinner itemsSpinner= (Spinner) findViewById(R.id.itemSpinner);
itemsSpinner.setAdapter(adapter);

To get the position of the item automatically add the following statement

要自动获取项目的位置,请添加以下语句

itemsSpinner.setSelection(itemsSpinner.getPosition("item2"));

回答by ZygoteInit

You can make a generic method for this kind of work as I do in my UtilityClass which is

你可以像我在我的 UtilityClass 中那样为这种工作创建一个通用方法,它是

public void SetSpinnerSelection(Spinner spinner,String[] array,String text) {
    for(int i=0;i<array.length;i++) {
        if(array[i].equals(text)) {
            spinner.setSelection(i);
        }
    }
}

回答by ZygoteInit

You can easily set like this: spinner.setSelection(1), instead of 1, you can set any position of list you would like to show.

您可以轻松地设置如下:spinner.setSelection(1),而不是 1,您可以设置您想要显示的列表的任何位置。

回答by pazfernando

I have a SimpleCursorAdapter so I have to duplicate the data for use the respose in this post. So, I recommend you try this way:

我有一个 SimpleCursorAdapter,所以我必须复制数据以使用这篇文章中的 respose。所以,我建议你试试这种方式:

for (int i = 0; i < spinnerRegion.getAdapter().getCount(); i++) {
    if (spinnerRegion.getItemIdAtPosition(i) == Integer
        .valueOf(signal.getInt(signal
            .getColumnIndexOrThrow("id_region")))) {
        spinnerRegion.setSelection(i);
        break;
    }
}

I think that is a real way

我认为这是一个真正的方式