1. The main function of adding adapters is to be able to add spinner data flexibly
1 Packagecom.jtl.spinner_2;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 6 ImportAndroid.os.Bundle;7 Importandroid.app.Activity;8 ImportAndroid.view.Menu;9 ImportAndroid.widget.ArrayAdapter;Ten ImportAndroid.widget.Spinner; One ImportAndroid.widget.SpinnerAdapter; A - Public classMainactivityextendsActivity { - the //define a Spinner object - //defines a list object that is used to store the contents of the drop-down menu options - Spinner SP1; - list List; + @Override - protected voidonCreate (Bundle savedinstancestate) { + Super. OnCreate (savedinstancestate); A Setcontentview (r.layout.activity_main); at - //list instantiated with Araaylist - //Drop- down menu Add content -list=NewArrayList (); -List.add ("Android Phase 1")); -List.add ("Android Phase 2")); inList.add ("Android Phase 3")); -List.add ("Android Phase 4")); toList.add ("Android phase 5")); +List.add ("Android phase 6")); -List.add ("Android phase 7")); the * $sp1=(Spinner) Findviewbyid (r.id.sp_1);Panax Notoginseng //Create a Spinneradapter adapter, -Spinneradapter adapter=NewArrayadapter (mainactivity. This,//Context Content theAndroid. R.layout.simple_list_item_1,//Add a drop- down menu layout file + /*Textviewresourceid,*/ //Add a drop- down menu layout file Alist);//Drop- down menu data source the //add an adapter to the spinner object + Sp1.setadapter (adapter); - } $ } $ //XML Layout File - /* - <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" the xmlns:tools= "Http://schemas.android.com/tools" - android:layout_width= "Match_parent"Wuyi android:layout_height= "Match_parent" the android:gravity= "center" - android:orientation= "Horizontal" > Wu - <textview android:layout_width= "100DP" About android:layout_height= "Wrap_content" $ android:text= "class:" - android:gravity= "center"/> - - <spinner A android:id= "@+id/sp_1" + android:layout_width= "100DP" the android:layout_height= "Wrap_content" - android:gravity= "center" $ /> the the </LinearLayout> the the - */
Usage of spinner _2 (add adapter)