What is the correct way to implement key-value pairs in Spinner in android

Is this the right way to implement a key-value pair for Spinner on 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. */ @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; } } 
+47
android
Mar 24 2018-11-21T00:
source share
4 answers

this is one way. I use it quite a lot, although I use my own adapter (inheriting from BaseAdpater). Another way would be, as indicated above, to have an index (0,1,2, etc.) of Mappd for the value, and when you get the element, it will index the well so that you can get its value for the map. I like this option less ...

+6
Mar 24 '11 at 10:14
source share

I created a hashMap adapter for use in these scenarios. Also see an example 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); 
+5
Sep 19 '15 at 5:48
source share

To avoid returning to items[] from the listener, use getItemAtPosition , which returns an object from the adapter. To access MyData methods, you must drop the object as follows:

 public void onItemSelected( AdapterView<?> parent, View view, int position, long id) { MyData d = (MyData) parent.getItemAtPosition(position); valueTextView.setText( d.getValue() ); } 
+3
Jun 29 '14 at 5:52
source share

Create a map of the key values ​​and take the value in onItemSelected (you can get the "key" through spinner.getAdapter (). GetItem (position)).

+2
Mar 24 '11 at 20:28
source share



All Articles