Android - 配置 Spinner 以使用数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1587028/
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
Android - configure Spinner to use array
提问by Bostone
I declare my Spinner in the following manner (it's very static so I
have 2 string arrays in array.xml
for titles and values)
我以下列方式声明我的 Spinner(它非常静态,所以我有 2 个字符串数组array.xml
用于标题和值)
<Spinner
android:id="@+id/searchCriteria"
android:entries="@array/searchBy"
android:entryValues="@array/searchByValues" />
I expect spinner.getSelectedItem()
to return an array [title, value]
but in fact it returns just a title String. Is it ignoring
android:entryValues
? How do I get a value, not a title from it? Is
this doable with XML only or do I need to create adapter and do it
programmatically?
我希望spinner.getSelectedItem()
返回一个数组,[title, value]
但实际上它只返回一个标题字符串。是无视
android:entryValues
吗?我如何获得一个价值,而不是一个标题?这仅适用于 XML 还是我需要创建适配器并以编程方式执行?
回答by KATR Software
Rather than the dual array method, why not fill your ArrayAdapter programmatically with objects of a known type and use that. I've written a tutorial of a similar nature (link at the bottom) that does this. The basic premise is to create an array of Java objects, tell the spinner about the, and then use those objects directly from the spinner class. In my example I have an object representing a "State" which is defined as follows:
而不是双数组方法,为什么不以编程方式用已知类型的对象填充您的 ArrayAdapter 并使用它。我写了一个类似性质的教程(底部的链接)来做这个。基本前提是创建一个 Java 对象数组,告诉微调器关于 ,然后直接从微调器类中使用这些对象。在我的示例中,我有一个表示“状态”的对象,其定义如下:
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 + ")" );
}
}
Then you can populate a spinner with an array of these classes as follows:
然后,您可以使用这些类的数组填充微调器,如下所示:
// 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);
You can retrieve the selected item as follows:
您可以按如下方式检索所选项目:
State st = (State)spinner.getSelectedItem();
And now you have a bona fide Java class to work with. If you want to intercept when the spinner value changes, just implement the OnItemSelectedListener and add the appropriate methods to handle the events.
现在您有一个真正的 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 )
{
}
You can find the whole tutorial here: http://www.katr.com/article_android_spinner01.php
你可以在这里找到整个教程:http: //www.katr.com/article_android_spinner01.php
回答by Bostone
So if you came here because you want to have both label and value in the Spinner - here's how I did it:
因此,如果您来到这里是因为您想在 Spinner 中同时拥有标签和价值 - 我是这样做的:
- Just create your Spinner the usual way
- Define 2 equal size arrays in your array.xml file. One for labels, one for values
- Set your Spinner with
android:entries="@array/labels"
In your code - when you need a value do something like this (no you don't have to chain it)
String selectedVal = getResources().getStringArray(R.array.values)[spinner .getSelectedItemPosition()];
- And remember - these 2 arrays have to match each other as far as number slots and positions
- 只需按照通常的方式创建 Spinner
- 在 array.xml 文件中定义 2 个相等大小的数组。一种用于标签,一种用于值
- 设置您的微调器
android:entries="@array/labels"
在您的代码中 - 当您需要一个值时,请执行以下操作(不,您不必链接它)
String selectedVal = getResources().getStringArray(R.array.values)[spinner .getSelectedItemPosition()];
- 请记住 - 这两个数组必须在数字槽和位置方面相互匹配
回答by Bostone
Abort, abort! I don't know what got into me but Spinner
does not support android:entryValues
attribute. That one is actually from ListPreference
which does a similar thing (displays list of items in pop-up dialog). For what I need I will have to (alas) use the SpinnerAdapter
中止,中止!我不知道是什么让我陷入了困境,但Spinner
不支持android:entryValues
属性。那个实际上是从ListPreference
它做类似的事情(在弹出对话框中显示项目列表)。对于我需要的东西,我将不得不(唉)使用SpinnerAdapter