An example analysis of key value pair usage for spinner control in Android _android

Source: Internet
Author: User
Tags gettext

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.

Related Article

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.