Android spinner implements text and Value

Source: Internet
Author: User

The implementation of text and value in mobile is very simple. We have done it in the control dorpdownlist, and we can get the value of text and value directly, how can I obtain the text and value values in Android? There are two ideas:

1. Use a custom data source to obtain the value of text and value;

2. Use a custom adapter to obtain the value of text and value.

In this article, we will use the first method to obtain the value of text and value, and bind the value selected in the spinner through value.

 

First, define the data structure of the spinner as follows:

/*** @ Author xushilin * XSL xushilin@kingtoneinfo.com * @ version: Creation Time: 11:12:06 * Description: Structure of the spinner data source * modification history: */public class spinnerdata {private string value = ""; private string text = ""; Public spinnerdata () {value = ""; text = "";} public spinnerdata (string _ value, string _ text) {value = _ value; text = _ text;} @ overridepublic string tostring () {return text;} Public String getvalue () {return value;} Public String gettext () {return text ;}}

The above structure contains two attributes: Value stores the value and text stores the data displayed to the user.

The following code creates a data instance for the spinner and binds it to the spinner:

Private void BIND () {list <spinnerdata> lst = new arraylist <spinnerdata> (); For (INT I = 0; I <10; I ++) {spinnerdata c = new spinnerdata (I + "", "display value" + I); lst. add (c);} arrayadapter <spinnerdata> adapter = new arrayadapter <spinnerdata> (this, android. r. layout. simple_spinner_item, LST); adapter. setdropdownviewresource (Android. r. layout. simple_spinner_dropdown_item); SP. setadapter (adapter );}

Obtain the instance control on the oncreate () interface and call the above BIND () method. Run the following command:

Click "get value" to obtain the value and text, as shown in the following figure:

Click the echo button. The selected text with value = 5 is as follows:

 

All code:

/*****/Package COM. demo. list; import Java. util. arraylist; import Java. util. list; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. arrayadapter; import android. widget. button; import android. widget. spinner; import android. widget. toast; import COM. demo. helloworld. r;/*** @ author XSL xushilin@kingtoneinfo.com * @ version: Creation Time: 09:56:43 am Description: modification history: */public class spinnerselectvalue extends activity implements onclicklistener {private spinner sp; private button btngetvalue, btnsetvalue; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. spinner_select_value); SP = (spinner) findviewbyid (R. id. spinner1); btngetvalue = (button) findviewbyid (R. id. btngetvalue); btngetvalue. setonclicklistener (this); btnsetvalue = (button) findviewbyid (R. id. btnsetvalue); btnsetvalue. setonclicklistener (this); BIND ();} private void BIND () {list <spinnerdata> lst = new arraylist <spinnerdata> (); For (INT I = 0; I <10; I ++) {spinnerdata c = new spinnerdata (I + "", "display value" + I); lst. add (c);} arrayadapter <spinnerdata> adapter = new arrayadapter <spinnerdata> (this, android. r. layout. simple_spinner_item, LST); adapter. setdropdownviewresource (Android. r. layout. simple_spinner_dropdown_item); SP. setadapter (adapter);}/*** @ author xushilin * XSL xushilin@kingtoneinfo.com * @ version: Creation Time: 11:12:06 * Description: Structure of the spinner data source * modification history: */public class spinnerdata {private string value = ""; private string text = ""; Public spinnerdata () {value = ""; text = "";} public spinnerdata (string _ value, string _ text) {value = _ value; text = _ text;} @ overridepublic string tostring () {return text;} Public String getvalue () {return value;} Public String gettext () {return text;} public void onclick (view v) {Switch (v. GETID () {case R. id. btngetvalue: String S = "value =" + (spinnerdata) sp. getselecteditem ()). getvalue () + "& text =" + (spinnerdata) sp. getselecteditem ()). gettext (); toast. maketext (this, S, toast. length_long ). show (); break; case R. id. btnsetvalue: @ suppresswarnings ("unchecked") arrayadapter <spinnerdata> lst = (arrayadapter <spinnerdata>) sp. getadapter (); For (INT I = 0; I <lst. getcount (); I ++) {If (lst. getitem (I ). getvalue (). equalsignorecase ("5") {sp. setselection (I); break ;}}}

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.