你如何获得一个微调的选定值? [英] How do you get the selected value of a Spinner?
问题描述
我想获得所选项目串出一个微调
的。到目前为止,我已经得到了这一点:
I am trying to get the selected items string out of a 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
?
推荐答案
要得到一个微调的选定值,你可以按照这个的例如的。
To get the selected value of a spinner you can follow this example.
创建实现AdapterView.OnItemSelectedListener嵌套类。这将提供一个项目时已选定的微调,将通知您的应用程序回调方法。
Create a nested class that implements AdapterView.OnItemSelectedListener. This will provide a callback method that will notify your application when an item has been selected from the Spinner.
在这一类的onItemSelected的方法,你可以选择的项目:
Within "onItemSelected" method of that class, you can get the selected item:
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.
}
}
最后,你ItemSelectedListener必须在微调注册的:
Finally, your ItemSelectedListener needs to be registered in the Spinner:
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
这篇关于你如何获得一个微调的选定值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!