Android Learning notes Control Spinner

Source: Internet
Author: User

1.mian.xml Layout file

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical" >6     7     <TextView8         Android:id= "@+id/textview1"9 Android:layout_width= "Match_parent"Ten Android:layout_height= "Wrap_content" One Android:text= "You are entering Arsenal" A android:textsize= "20SP"/> -      -     <Spinner the         Android:id= "@+id/spinner1" - Android:layout_margintop= "10DP" - Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content"/> +      -  + </LinearLayout>

2.item.xml Style layout file

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "Horizontal" >6     7     <ImageView8         Android:id= "@+id/imageview1"9 Android:layout_width= "60DP"Ten Android:layout_height= "60DP" One android:src= "@drawable /A"/> A      -     <TextView -         Android:id= "@+id/textview2" the Android:layout_width= "Match_parent" - Android:layout_height= "60DP" - Android:text= "Arsenal" - android:textsize= "20SP" + android:gravity= "Center"/> -      +  A </LinearLayout>

3.java Code

1  Public classMainactivityextendsActivity {2     3     PrivateTextView TV1;4     PrivateSpinner S1;5     PrivateList<map<string,object>>dataList;6     Privatesimpleadapter sa;7     Private int[] pic ={R.DRAWABLE.A,R.DRAWABLE.L,R.DRAWABLE.M,R.DRAWABLE.Q};8     PrivateString[] name = {"Arsenal", "Liverpool", "Manchester United", "Chelsea"};9 Ten @Override One     protected voidonCreate (Bundle savedinstancestate) { A         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.main); -          theTV1 =(TextView) Findviewbyid (r.id.textview1); -S1 =(Spinner) Findviewbyid (r.id.spinner1); -          -DataList =NewArraylist<map<string,object>>(); +          -         //New Adapter +SA =NewSimpleadapter ( This, GetData (), R.layout.item,Newstring[]{"pic", "name"},New int[]{r.id.imageview1,r.id.textview2}]; A          at         //set a drop-down style sheet - Sa.setdropdownviewresource (r.layout.item); - S1.setadapter (SA); -          -         //S1 setting up listeners -S1.setonitemselectedlistener (NewOnitemselectedlistener () { in  - @Override to              Public voidOnitemselected (adapterview<?>Parent, view view, +                     intPositionLongID) { -                 //TODO auto-generated Method Stub theString team =Sa.getitem (position). toString (); *Tv1.settext ("You entered is" +team); $             }Panax Notoginseng  - @Override the              Public voidOnnothingselected (adapterview<?>parent) { +                 //TODO auto-generated Method Stub A                  the             } +         }); -     } $     //set up a data source $     PrivateList<map<string,object>>GetData () { -          for(inti = 0;i<pic.length;i++){ -map<string, object> map =NewHashmap<string, object>(); theMap.put ("Pic", Pic[i]); -Map.put ("name", Name[i]);Wuyi datalist.add (map); the         } -         returndataList; Wu     } -      About}

Android Learning notes Control Spinner

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.