What's the correct way to implement key-value pair in Spinner in android

Is this the correct way to implement key-value pair for a Spinner in Android?

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. */
    public void onCreate(Bundle savedInstanceState)

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

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

this is one way.
i use it quite a lot though i use my own adapter (inheriting from BaseAdpater).
Another way would b like the above to have an index (0,1,2 etc ) mappd to a value and when you get an item get it’s index a well so you can retrieve it’s value fro mthe map.
I like that option less…

Solution 2

I created an HashMap adapter for use in these scenarios. Also see example project 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);

    spinner = (Spinner) findViewById(R.id.spinner);

Solution 3

To avoid reaching back into items[] from the listener use getItemAtPosition which returns an Object from the Adapter. To access the MyData methods you must cast the object like so:

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

Solution 4

Create a map of key-values, and take a value in onItemSelected(you can obtain “key” via spinner.getAdapter().getItem(position)).

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply