In Android Spinner, the value and text values are implemented

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.