我以以下方式声明我的微调器(它非常静态,所以我 有 2 个字符串数组 array.xml 对于标题和值)

<Spinner
    android:id="@+id/searchCriteria"
    android:entries="@array/searchBy"
    android:entryValues="@array/searchByValues" />

我预计 spinner.getSelectedItem() 返回一个数组 [title, value]但实际上它只返回一个标题字符串。是否无视android:entryValues?我如何从中获取值而不是标题?是 这仅适用于 XML,还是我需要创建适配器并执行此操作 编程?

有帮助吗?

解决方案

不是双阵列的方法,为什么不具有已知类型,并使用该对象编程填补你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 + ")" );
}
}

然后,可以如下填充这些类的阵列的旋转器:

       // 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);  

可以如下检索选定项目:

State st = (State)spinner.getSelectedItem();

和现在你有一个真正的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 ) 
{ 
}

您可以在这里找到完整的教程: http://www.katr.com/article_android_spinner01.php

其他提示

因此,如果您来到这里是因为您想在 Spinner 中同时拥有标签和值 - 这就是我的做法:

  1. 只需按照通常的方式创建 Spinner
  2. 在 array.xml 文件中定义 2 个大小相等的数组。一种用于标签,一种用于值
  3. 设置你的微调器 android:entries="@array/labels"
  4. 在您的代码中 - 当您需要一个值时,请执行类似的操作(不,您不必链接它)

    String selectedVal = getResources().getStringArray(R.array.values)[spinner
                             .getSelectedItemPosition()];
    

  5. 请记住 - 这两个数组必须在数字槽和位置方面相互匹配

中止,中止!我不知道是什么钻进了我,但Spinner不支持android:entryValues属性。这一个实际上是从ListPreference它做了类似的事情(在弹出的对话框项目的显示列表)。我需要什么,我将不得不(唉)使用SpinnerAdapter

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top