Android的 - 配置微调使用数组 [英] Android - configure Spinner to use array

查看:141
本文介绍了Android的 - 配置微调使用数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我宣布我的微调以下列方式(这是非常静态的,所以我 在 array.xml 2的字符串数组的标题和值)

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 android:id="@+id/searchCriteria" android:entries="@array/
searchBy" android:entryValues="@array/searchByValues">
</Spinner>

我希望 spinner.getSelectedItem()返回数组 [标题,值] 但事实上它返回只是一个标题字符串。它忽略 安卓entryValues​​ ?我如何得到一个值,而不是从它的标题?是 这是可行的,只有XML或做我需要创建适配器,这样做 编程?

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?

推荐答案

而不是双阵列的方法,为什么不与已知类型的对象编程填写您的ArrayAdapter,并使用它。我写了一个类似性质(底部链接),这是否一个教程。基本premise是创建Java对象的数组,讲述了微调,然后直接使用这些对象从微调类。在我的例子我已经重新$ P $对象psenting一个国家的定义如下:

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:

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

现在,你有工作,一个真正的Java类。如果你想拦截,当微调值发生变化,只需实现OnItemSelectedListener并添加相应的方法来处理事件。

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.

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

You can find the whole tutorial here: http://www.katr.com/article_android_spinner01.php

这篇关于Android的 - 配置微调使用数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆