Bos*_*one 47 arrays android spinner android-spinner
我以下面的方式声明我的Spinner(它非常静态,所以我有2个字符串数组array.xml
用于标题和值)
<Spinner
android:id="@+id/searchCriteria"
android:entries="@array/searchBy"
android:entryValues="@array/searchByValues" />
Run Code Online (Sandbox Code Playgroud)
我希望spinner.getSelectedItem()
返回一个数组,[title, value]
但事实上它只返回一个标题字符串.它无视
android:entryValues
吗?我如何获得价值,而不是它的标题?这仅适用于XML还是我需要创建适配器并以编程方式执行?
小智 129
为什么不以编程方式用已知类型的对象填充ArrayAdapter并使用它,而不是双数组方法.我写了一个类似性质的教程(链接在底部),这样做.基本前提是创建一个Java对象数组,告诉微调器,然后直接从spinner类中使用这些对象.在我的例子中,我有一个表示"状态"的对象,其定义如下:
package com.katr.spinnerdemo;
public class State {
// Okay, full acknowledgment that public members are not a good idea, however
// this is a Spinner demo not an exercise in java best practices.
public int id = 0;
public String name = "";
public String abbrev = "";
// A simple constructor for populating our member variables for this tutorial.
public State( int _id, String _name, String _abbrev )
{
id = _id;
name = _name;
abbrev = _abbrev;
}
// The toString method is extremely important to making this class work with a Spinner
// (or ListView) object because this is the method called when it is trying to represent
// this object within the control. If you do not have a toString() method, you WILL
// get an exception.
public String toString()
{
return( name + " (" + abbrev + ")" );
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用这些类的数组填充微调器,如下所示:
// Step 1: Locate our spinner control and save it to the class for convenience
// You could get it every time, I'm just being lazy... :-)
spinner = (Spinner)this.findViewById(R.id.Spinner01);
// Step 2: Create and fill an ArrayAdapter with a bunch of "State" objects
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, new State[] {
new State( 1, "Minnesota", "MN" ),
new State( 99, "Wisconsin", "WI" ),
new State( 53, "Utah", "UT" ),
new State( 153, "Texas", "TX" )
});
// Step 3: Tell the spinner about our adapter
spinner.setAdapter(spinnerArrayAdapter);
Run Code Online (Sandbox Code Playgroud)
您可以按如下方式检索所选项目:
State st = (State)spinner.getSelectedItem();
Run Code Online (Sandbox Code Playgroud)
现在你有一个真正的Java类可以使用.如果要在微调器值更改时进行拦截,只需实现OnItemSelectedListener并添加适当的方法来处理事件.
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
// Get the currently selected State object from the spinner
State st = (State)spinner.getSelectedItem();
// Now do something with it.
}
public void onNothingSelected(AdapterView<?> parent )
{
}
Run Code Online (Sandbox Code Playgroud)
您可以在此处找到整个教程:http: //www.katr.com/article_android_spinner01.php
Bos*_*one 54
所以如果你来到这里是因为你想在Spinner中同时拥有标签和值 - 这就是我如何做到的:
android:entries="@array/labels"
在你的代码中 - 当你需要一个值时做这样的事情(不,你不必链接它)
String selectedVal = getResources().getStringArray(R.array.values)[spinner
.getSelectedItemPosition()];
Run Code Online (Sandbox Code Playgroud)
中止,中止!我不知道是什么进入我但Spinner
不支持android:entryValues
属性.实际上ListPreference
是一个类似的东西(在弹出对话框中显示项目列表).为了我需要,我将不得不(唉)使用SpinnerAdapter