简体   繁体   English

如何获得 Spinner 的选定值?

[英]How do you get the selected value of a Spinner?

I am trying to get the selected items string out of a Spinner .我正在尝试从Spinner中获取选定的项目字符串。 So far I have gotten this:到目前为止,我已经得到了这个:

bundle.putString(ListDbAdapter.DB_PRI, v.getText().toString());

This does not work and gives a class casting exception (I thought I could cast a View to a widget that inherits it. Obviously not!) So how do you get the selected value of a Spinner ?这不起作用并给出类转换异常(我认为我可以将View转换为继承它的小部件。显然不是!)那么如何获得Spinner的选定值?

To get the selected value of a spinner you can follow this example .要获取微调器的选定值,您可以按照此示例进行操作。

Create a nested class that implements AdapterView.OnItemSelectedListener.创建一个实现 AdapterView.OnItemSelectedListener 的嵌套类。 This will provide a callback method that will notify your application when an item has been selected from the Spinner.这将提供一个回调方法,该方法将在从 Spinner 中选择项目时通知您的应用程序。

Within "onItemSelected" method of that class, you can get the selected item:在该类的“onItemSelected”方法中,您可以获得所选项目:

public class YourItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        String selected = parent.getItemAtPosition(pos).toString();
    }

    public void onNothingSelected(AdapterView parent) {
        // Do nothing.
    }
}

Finally, your ItemSelectedListener needs to be registered in the Spinner:最后,您的 ItemSelectedListener 需要在 Spinner 中注册:

spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

You have getSelectedXXX methods from the AdapterView class from which the Spinner derives:您有来自 Spinner 派生的 AdapterView 类的 getSelectedXXX 方法:

getSelectedItem() 获取选定项()

getSelectedItemPosition() getSelectedItemPosition()

getSelectedItemId() getSelectedItemId()

Simply use this:只需使用这个:

spinner.getItemAtPosition(spinner.getSelectedItemPosition()).toString();

This will give you the String of the selected item in the Spinner .这将为您提供Spinner中所选项目的String

mySpinner.getItemAtPosition(mySpinner.getSelectedItemPosition())根据 Rich 的描述工作。

Depends ay which point you wish to "catch" the value.取决于您希望“捕捉”该值的哪一点。

For instance, if you want to catch the value as soon as the user changes the spinner selected item, use the listener approach (provided by jalopaba)例如,如果您想在用户更改微调器选定项时立即捕获值,请使用侦听器方法(由 jalopaba 提供)

If you rather catch the value when a user performs the final task like clicking a Submit button, or something, then the answer provided by Rich is better.如果您更愿意在用户执行最终任务(例如单击提交按钮或其他内容)时捕获该值,那么 Rich 提供的答案会更好。

This is another way:这是另一种方式:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int pos, long arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

要仅获取微调器中的字符串值,请使用以下命令:

spinner.getSelectedItem().toString();
Spinner spinner=(Spinner) findViewById(R.id.spinnername);
String valueinString = spinner.getSelectedItem().toString();

In Case Spinner values are int the typecast it to int如果 Spinner 值是int ,则将其类型转换为int

int valueinInt=(int)(spinner.getSelectedItem());

Useful code/syntax to simply get the selected index/value and assign it to a sutiable variable;有用的代码/语法可以简单地获取选定的索引/值并将其分配给一个合适的变量;

int i = ((Spinner)findViewById(R.id.spinnerDistance)).getSelectedItemPosition();
String s = ((Spinner)findViewById(R.id.spinnerDistance)).getSelectedItem().toString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM