Key-Value Pair usage of Android Spinner Control

Source: Internet
Author: User

I. dictionary table used to store key-value pairs

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 rewrite toString?
*
* When the adapter displays data, if the object passed into the adapter is not a string, the object. toString () is directly used ()
*/
@ Override
Public String toString (){
Return text;
}

}

Ii. activity Class: bind data and obtain 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 ){
// Method for obtaining the key: mySpinner. getSelectedItem (). toString () or (Dict) mySpinner. getSelectedItem (). getId ()
// Method for obtaining the value: (Dict) mySpinner. getSelectedItem (). getText ();
Toast. makeText (MainActivity. this,
"Key:" + mySpinner. getSelectedItem (). toString () + "," + (Dict) mySpinner. getSelectedItem (). getId () +
", Value:" + (Dict) mySpinner. getSelectedItem (). getText (),
Toast. LENGTH_LONG). show ();
}

Public void onNothingSelected (AdapterView <?> Parent ){

}
});
}
}

3. Modify 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>

4. The running result is as follows:

 

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.