The. XML code is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" horizontal "android:layout_width=" Fill_parent "android:layout_height=" Fill_parent " ><spinnerandroid:id= "@+id/city" android:prompt= "@string/city_prompt" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:entries= "@array/city_lables"/><spinnerandroid:id= "@+id/area" android:prompt= "@string/city_prompt" android:layout_width= "wrap_content" android:layout_height= "wrap_content"/ ></LinearLayout>
. Java code is as follows:
Package Org.lxh.demo;import Android.app.activity;import Android.os.bundle;import android.view.view;import Android.widget.adapterview;import Android.widget.adapterview.onitemselectedlistener;import Android.widget.arrayadapter;import Android.widget.spinner;public class Myspinnerlistenerdemo extends Activity { Private Spinner city = null; Drop-down list box contents private Spinner area = null; Level two list private string[][] Areadata = new string[][] {{"Dongcheng", "Xicheng", "Chaoyang", "Daxing", "Pinggu"},//For the first level of the sub-information {"Huangpu", "Yangpu", "Minhang"},/ /For sub-information of level two {"Guangzhou"}//for sub-information of level three}; Child menu item Private arrayadapter<charsequence> adapterarea = null; @Overridepublic void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Super.setcontentview (r.layout.main); this.city = (Spinner) Super.findviewbyid (r.id.city); Get drop-down list box This.area = (Spinner) Super.findviewbyid (R.id.area); Gets the drop-down list box This.city.setOnItemSelectedListener (New Onitemselectedlistenerimpl ());} Private class Onitemselectedlistenerimpl implements Onitemselectedlistener {public void onitemselected (adapterview<?> parent, View view,int position, long ID) {//= trigger when option is changed Myspinnerlisten ErDemo.this.adapterArea = new Arrayadapter<charsequence> (myspinnerlistenerdemo.this,android. R.layout.simple_spinner_item,myspinnerlistenerdemo.this.areadata[position]); Define all list Items MySpinnerListenerDemo.this.area.setAdapter (MySpinnerListenerDemo.this.adapterArea);//SET option content for level two drop-down list} public void onnothingselected (adapterview<?> arg0) {//indicates that there is no option to trigger//Generally this method does not care now}}}
The effect is as follows:
SOURCE Download: http://download.csdn.net/detail/yayun0516/8363843
Android implementation drop-down list cascade