This example describes the use of key-value pairs for spinner controls in Android. Share to everyone for your reference. Specifically as follows:
A dictionary table for storing key-value pairs of information
Package com.ljq.activity;
Import java.io.Serializable;
@SuppressWarnings ("Serial") Public
class Dict implements Serializable {
private Integer ID;
private String text;
Public Dict () {
} public
Dict (Integer ID, String text) {
super ();
This.id = ID;
This.text = text;
Public Integer GetId () {return
ID;
}
public void SetId (Integer id) {
this.id = ID;
}
Public String GetText () {return
text;
}
public void SetText (String text) {
this.text = text;
}
/**
* Why do you want to rewrite ToString ()?
*
* Because the adapter is displaying the data, the object is used directly if the object of the incoming adapter is not a string. toString ()/
@Override public
string ToString () {return
text;
}
}
Activity class, binding data, getting the selected key value pair
Package com.ljq.activity;
Import java.util.ArrayList;
Import java.util.List;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.AdapterView;
Import Android.widget.ArrayAdapter;
Import Android.widget.Spinner;
Import Android.widget.Toast;
Import Android.widget.AdapterView.OnItemSelectedListener;
public class Mainactivity extends activity {private Spinner myspinner;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Myspinner = (Spinner) Findviewbyid (R.id.myspinner);
list<dict> dicts = new arraylist<dict> ();
Dicts.add (New Dict (1, "Test 1"));
Dicts.add (New Dict (2, "Test 2"));
Dicts.add (New Dict (3, "Test 3"));
Dicts.add (New Dict (4, "Test 4")); arrayadapter<dict> adapter = new Arrayadapter<dict> (this, Android.
R.layout.simple_spinner_item, dicts);
Myspinner.setadapter (adapter); Myspinner.setonitemselectedlistener (New OnitemsElectedlistener () {public void onitemselected (adapterview<?> parent, view view, int position, long ID) { Gets the method of the key: Myspinner.getselecteditem (). toString () or ((Dict) Myspinner.getselecteditem ()). GetId ()//Get Value Method: ((Dict)
Myspinner.getselecteditem ()). GetText (); Toast.maketext (Mainactivity.this, "key:" + Myspinner.getselecteditem (). toString () + "," + (Dict) myspinner.getselecte
Ditem ()). GetId () + ", Value:" + ((Dict) Myspinner.getselecteditem ()). GetText (), Toast.length_long). Show ();
public void onnothingselected (adapterview<?> parent) {}});
}
}
Iii. Modifying the Main.xml layout file
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:orientation=" vertical "
android:layout_width=" fill_parent "
android:layout_" height= "Fill_parent" >
<spinner android:layout_width= "wrap_content" android:layout_height= "WRAP_"
Content "
android:id=" @+id/myspinner "/>
</LinearLayout>
Four, the results of the operation are as follows:
I hope this article will help you with your Android program.