The spinner control is used to display a drop-down list that needs to create a adapter adapter object when loading data. And in the process of creating the adapter object, you specify that the data to be mounted is an array or a list object, and the following is the spinner instance: in the Main.xml file:
1<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3android:orientation= "Vertical"4android:layout_height= "Fill_parent"5Android:layout_width= "Fill_parent" >6<Spinner7 android:id= "@+id/spinner1"8Android:layout_width= "Fill_parent"9android:layout_height= "Wrap_content"/>Ten<Spinner One android:id= "@+id/spinner2" AAndroid:layout_width= "Fill_parent" -android:layout_height= "Wrap_content"/> -</LinearLayout>
In the Item.xml file:
1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:orientation= "Horizontal" >6<imageview android:id= "@+id/imageview" android:layout_width= "60DP"7android:layout_height= "60DP" android:src= "@drawable/ic_launcher"8android:paddingleft= "10DP" ></ImageView>9<textview android:id= "@+id/textview" android:textcolor= "#000"TenAndroid:layout_width= "Wrap_content" android:layout_height= "Fill_parent" OneAndroid:textsize= "16DP" android:gravity= "center_vertical" Aandroid:paddingleft= "10DP" ></TextView> -</LinearLayout>
In the. java file:
1 PrivateSpinner Spinner;2 PrivateSpinner Spinner2;3 @Override4 protected voidonCreate (Bundle savedinstancestate) {5 Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.main);7Spinner =(Spinner) Findviewbyid (r.id.spinner1);8arrayadapter< string> adapter =NewArrayadapter<string> (mainactivity. This,Android. R.layout.simple_spinner_item,Newstring[]{"AA", "BB", "CC", "DD"});9 Spinner.setadapter (adapter); Self-contained layouts in Android systemsTen OneSpinner2 =(Spinner) Findviewbyid (r.id.spinner2); A Finallist<map<string, object>> data =NewArraylist<map<string,object>>(); -map<string, Object > Map =NewHashmap<string, object>(); -Map.put ("name", "GIRL1"); theMap.put ("image", R.DRAWABLE.GRIL1); - data.add (map); -Map =NewHashmap<string, object>(); -Map.put ("name", "Girl2"); +Map.put ("image", R.DRAWABLE.GRIL2);//The image ID address is loaded into the map, and the ImageView in the layout automatically displays the corresponding picture - data.add (map); +Map =NewHashmap<string, object>(); AMap.put ("name", "Gir3"); atMap.put ("image", R.DRAWABLE.GRIL3); - data.add (map); -Simpleadapter Adapter2 =NewSimpleadapter (mainactivity. This, data,R.layout.item,Newstring[]{"image", "Name"},New int[]{r.id.imageview,r.id.textview}]; - Spinner2.setadapter (ADAPTER2); Custom Layouts -Spinner2.setonitemselectedlistener (NewOnitemselectedlistener () { - in @Override - Public voidOnitemselected (adapterview<?>Parent, view view, to intPositionLongID) { + //TODO Auto-generated method stubs -map<string, object> item =Data.get (position); theSettitle (Item.get ("name"). toString ()); * } $ Panax Notoginseng @Override - Public voidOnnothingselected (adapterview<?>parent) { the //TODO Auto-generated method stubs + A } the }); +}
Operation Result:
Spinner drop-down list control