This two-day study of the use of simpleadapter and spinner implementation of the drop-down list of functions, the code is now shared with you:
Code Listing 1-Add a file named "Selectactivity.java":
Package Com.ghj.activity;import Java.util.arraylist;import Java.util.hashmap;import java.util.list;import Java.util.map;import Android.app.activity;import Android.os.bundle;import Android.view.view;import Android.widget.adapterview;import Android.widget.simpleadapter;import Android.widget.spinner;import Android.widget.textview;import Android.widget.toast;public class Selectactivity extends Activity {private Spinner Spinner;private simpleadapter simpleadapter; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.select); spinner = (spinner) Findviewbyid (R.id.spinner) ; list<map<string, string>> specialtylist = Getspinnerdata (); simpleadapter = new SimpleAdapter (This, Specialtylist, R.layout.select_item, new string[]{"id", "name"}, new Int[]{r.id.value, r.id.lable}); Spinner.setadapter (Simpleadapter); Setdefaultspinner (Specialtylist, "Yingyu");// Sets the default check Spinner.setonitemselectedlistener (new Adapterview.onitemselectedliStener () {@Overridepublic void onitemselected (adapterview<?> parent, view view, int position, long id) {String lable = ((TextView) SelectActivity.this.findViewById (r.id.lable)). GetText (). toString (); String value = ((TextView) SelectActivity.this.findViewById (R.id.value)). GetText (). toString (); Toast.maketext (selectactivity.this, lable + "→" + value, Toast.length_long). Show (); @Overridepublic void onnothingselected (adapterview<?> parent) {}}); /** * Set drop-down list default Check item * * @author Gao Yingjie */private void Setdefaultspinner (list<map<string, string>> maplist, String ID) {for (int i = 0; i < maplist.size (); i++) {map<string, string> Map = Maplist.get (i); if (Id.equals ("id") ) {spinner.setselection (i,true); break;}}} Private list<map<string, string>> Getspinnerdata () {list<map<string, string>> specialtyList = New arraylist<map<string, string>> (); map<string, string> map1 = new hashmap<string, string> (); Map1.put ("id", "Yuwen");Map1.put ("name", "language"); Specialtylist.add (MAP1); map<string, string> map2 = new hashmap<string, string> (); Map2.put ("id", "Shuxue"); Map2.put ("Name", "mathematics"); Specialtylist.add (MAP2); map<string, string> map3 = new hashmap<string, string> (); Map3.put ("id", "Yingyu"); Map3.put ("Name", "English"); Specialtylist.add (MAP3); return specialtylist;}}
Code Listing 2-Add a file named "Select_item_selector.xml":
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:state_pressed=" true "> <shape> <solid android:color=" # 60ffa500 "/> </shape> </item> <item android:state_focused=" true "> <shape > <solid android:color= "#60FFA500"/> </shape></item></selector>
Code Listing 3-Add a file named "Select.xml":
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_ Parent " android:layout_height=" match_parent "> <spinner android:id=" @+id/spinner " Android : layout_width= "fill_parent" android:layout_height= "Wrap_content"/></linearlayout>
Code Listing 4-Add a file named "Select_item.xml":
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" Match_parent " android: background= "@drawable/select_item_selector" android:orientation= "vertical" > <textview Android:id= "@+id/lable" android:layout_width= "fill_parent" android:layout_height= "45DP" android: Gravity= "center_vertical" android:paddingleft= "12DP"/> <textview android:id= "@+id/value" android:layout_width= "wrap_content" android:layout_height= "match_parent" android:visibility= " Gone "/></linearlayout>
Code Listing 5-Modifying a file named "Styles.xml":
<resources> <!-- Base application theme, dependent on API level. This theme are replaced by Appbasetheme from Res/values-vxx/styles.xml on newer devices. -- <style name= "Appbasetheme" parent= "Android:Theme.Light" > <!-- Theme customizations Available in newer API levels can go in Res/values-vxx/styles.xml, while customizations related to Backward-compa Tibility can go here. -</style> <!--application Theme-- <style name= "Apptheme" parent= "Android: Theme.light "> <!--all customizations. is not specific to a particular api-level can go. </style></resources>
【
0 min Download Example】
How to implement a drop-down list with Simpleadapter and spinner