Android implementation drop-down list cascade

Source: Internet
Author: User

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

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.