什么是在Android中的Spinner中实现键值对的正确方法

时间:2011-03-24 20:02:29

标签: android

这是在Android中为Spinner实现键值对的正确方法吗?

package com.mypackage

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Spinner;
import android.widget.ArrayAdapter;
import android.widget.AdapterView;
import android.widget.TextView;

public class SpinnerAndAdapter extends Activity
{
    TextView    valueTextView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        valueTextView = (TextView)findViewById( R.id.selected );
        Spinner s = (Spinner)findViewById(R.id.spinner);
        final MyData items[] = new MyData[3];
        items[0] = new MyData( "key1","value1" );
        items[1] = new MyData( "key2","value2" );
        items[2] = new MyData( "key3","value3" );
        ArrayAdapter<MyData> adapter = 
            new ArrayAdapter<MyData>( 
                this,
                android.R.layout.simple_spinner_item,
                items );
        adapter.setDropDownViewResource(
            android.R.layout.simple_spinner_dropdown_item);
        s.setAdapter(adapter);
        s.setOnItemSelectedListener(
            new AdapterView.OnItemSelectedListener() {
                public void onItemSelected(
                        AdapterView<?> parent, 
                        View view, 
                        int position, 
                        long id) {
                    MyData d = items[position];
                    valueTextView.setText( d.getValue() );
                }

                public void onNothingSelected(AdapterView<?> parent) {
                }
            }
        );
    }

    class MyData {
        public MyData( String spinnerText, String value ) {
            this.spinnerText = spinnerText;
            this.value = value;
        }

        public String getSpinnerText() {
            return spinnerText;
        }

        public String getValue() {
            return value;
        }

        public String toString() {
            return spinnerText;
        }

        String spinnerText;
        String value;
    }
}

4 个答案:

答案 0 :(得分:6)

这是一种方式。 虽然我使用自己的适配器(继承自BaseAdpater),但我使用它非常多。 另一种方式是像上面那样有一个索引(0,1,2等)mappd到一个值,当你得到一个项目得到它的索引一个井,所以你可以从地图中检索它的值。 我更喜欢那个选项...

答案 1 :(得分:5)

我创建了一个HashMap adapter,用于这些场景。另请参见示例项目here

    mapData = new LinkedHashMap<String, String>();

    mapData.put("shamu", "Nexus 6");
    mapData.put("fugu", "Nexus Player");
    mapData.put("volantisg", "Nexus 9 (LTE)");
    mapData.put("volantis", "Nexus 9 (Wi-Fi)");
    mapData.put("hammerhead", "Nexus 5 (GSM/LTE)");
    mapData.put("razor", "Nexus 7 [2013] (Wi-Fi)");
    mapData.put("razorg", "Nexus 7 [2013] (Mobile)");
    mapData.put("mantaray", "Nexus 10");
    mapData.put("occam", "Nexus 4");
    mapData.put("nakasi", "Nexus 7 (Wi-Fi)");
    mapData.put("nakasig", "Nexus 7 (Mobile)");
    mapData.put("tungsten", "Nexus Q");

    adapter = new LinkedHashMapAdapter<String, String>(this, android.R.layout.simple_spinner_item, mapData);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinner = (Spinner) findViewById(R.id.spinner);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);

答案 2 :(得分:3)

为避免从侦听器返回items[],请使用getItemAtPosition从适配器返回一个Object。要访问MyData方法,您必须像这样强制转换对象:

public void onItemSelected(
        AdapterView<?> parent, View view, int position, long id) {
    MyData d = (MyData) parent.getItemAtPosition(position);
    valueTextView.setText( d.getValue() );
}

答案 3 :(得分:2)

创建键值映射,并在onItemSelected中取值(您可以通过spinner.getAdapter()获取“key”.getItem(position))。