Android Development Step-by-step 23:spinner binding an object containing key and value

Source: Internet
Author: User

Spinner generally binds an array of strings, such as the previous practice arrayadapter<charsequence> adapter = Arrayadapter.createfromresource (
This, R.array.fruit, Android. R.layout.simple_spinner_item); sometimes we need to bind an object, such as display, to display value, but the program needs to get the key value, which is when we need to bind the custom object. Test Scenario: Document Type drop-down box, display to the user to see the ID card, passport, hukou and so on text, but we need to record the database is the corresponding key such as 0,1,2.
First, the design activity spinnerkeyvalueactivity
/**
*
*/
Package Com.figo.helloworld;

Import java.util.ArrayList;
Import java.util.List;
Import Android.view.View;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemSelectedListener;
Import Android.widget.ArrayAdapter;
Import Android.widget.Spinner;
Import Android.widget.TextView;

/**
* @author Zhuzhifei
*
*/
public class Spinnerkeyvalueactivity extends Activity {

Private Spinner Spcerticatetype;
Private TextView Tvcerticatetype;
@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Settings page
Setcontentview (R.layout.spinnerkeyvalue);
Spcerticatetype= (Spinner) Findviewbyid (R.id.spcerticatetype);
Tvcerticatetype= (TextView) Findviewbyid (R.id.tvcerticatetype);
Bind data to Spinner
Binddata ();
}
Bind data to Spinner
private void Binddata ()
{
Set Document Type
list<spinneritem> lst = new arraylist<spinneritem> ();
Spinneritem item0 = new Spinneritem ("0", "identity card");
Spinneritem item1 = new Spinneritem ("1", "passport");
Spinneritem item2 = new Spinneritem ("2", "Army Badge");
Spinneritem item3 = new Spinneritem ("3", "Soldier Badge");
Spinneritem item4 = new Spinneritem ("4", "Hometown Card");
Spinneritem ITEM5 = new Spinneritem ("5", "Temporary ID");
Spinneritem item6 = new Spinneritem ("6", "hukou");
Spinneritem Item7 = new Spinneritem ("7", "other");
Spinneritem ITEM8 = new Spinneritem ("8", "Alien residence permit");
Spinneritem item9 = new Spinneritem ("9", "Police badge");
Lst.add (ITEM0);
Lst.add (ITEM1);
Lst.add (ITEM2);
Lst.add (ITEM3);
Lst.add (ITEM4);
Lst.add (ITEM5);
Lst.add (ITEM6);
Lst.add (ITEM7);
Lst.add (ITEM8);
Lst.add (ITEM9);
arrayadapter<spinneritem> aradapter = new Arrayadapter<spinneritem> (
This.getapplicationcontext (),
Android. R.layout.simple_spinner_item, LST);
Aradapter
. Setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item);
Spcerticatetype.setadapter (Aradapter);
When an option is selected, add an event
Spcerticatetype.setonitemselectedlistener (

New Onitemselectedlistener () {
public void onitemselected (
Adapterview<?> Parent, view view, int position, long ID) {
Spinneritem item = (Spinneritem) spcerticatetype.getselecteditem ();
Tvcerticatetype.settext ("You have selected: key=" +item. key+ ", value=" +item. Value);
}
public void onnothingselected (adapterview<?> parent) {
}

});


}
Define drop-down box options
public class Spinneritem {
Private String Key = "";
Private String Value = "";

Public Spinneritem () {
Key = "";
Value = "";
}

Public Spinneritem (String _key, string _value) {
Key = _key;
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 ()
TODO auto-generated Method Stub
return Value;
}

Public String GetKey () {
return Key;
}

Public String GetValue () {
return Value;
}
}

}

Second, the Operation effect


Android Development Step-by-step 23:spinner binding an object containing key and value

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.