In order to achieve the spinner implementation of the Android value and text values, I tried a number of ways, in the online search of information, are said to modify the adapter, just at the beginning I also by modifying the adapter method to do, but if an activity has multiple spinner, You need to declare more than a few adapters global variables. After looking at the source code discovery of the adapter, in fact, there is no need to modify the adapter, directly to the adapter inside the object, and then rewrite the object's ToString () method can be easily resolved. Gossip less, paste code:
Declare an object first, and override the object's ToString ().
public class citem { private string id = ""; private String Value = ""; public citem () { ID = ""; Value = ""; } public citem (String _id, string _value) { id = _ID; Value = _Value; } @Override public string tostring () { //Why do you want to rewrite ToString ()? Because the adapter displays the data, the object is used directly if the incoming adapter object is not a string. toString () return Value; } public string getid () { return ID; } public string getvalue () { return value; }}
Then declare the object instance and add it to the ArrayList, and set the spinner adapter
Spinner Sp = (Spinner) .....//list<citem > LST = new arraylist<citem> (); CItem ct = new CItem ("1", "Test"), LST. ADD (CT);; Arrayadapter<citem > Adapter = new arrayadapter<citem> (context, Android. R.layout.simple_spinner_item, LST); Sp.setadapter (Adapter);
Value:
If you take the text value you can take it directly: Sp.getSelectedItem.ToString () or: ((CItem) sp.getselecteditem). GetValue ();
If you go to value, you can take it this way: ((CItem) sp.getselecteditem). GetID ();
Writing is relatively simple, you should be able to read.
In Android Spinner, the value and text values are implemented