Android implementation three-level linkage drop-down box drop-down list spinner instances

Source: Internet
Author: User

Main implementation: Dynamically load all levels drop-down values of the adapter

In the listen to this level drop-down box, when the selected value of this level drop-down box changes, the binding value of the subordinate adapter is modified with it

XML layout:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Wrap_content "    android:orientation= "Horizontal"    android:padding= "8DP" >    <spinner        android:id= "@+id/spin_ Province "        android:layout_width=" 80DP "        android:layout_height=" wrap_content "/>    <spinner        Android:id= "@+id/spin_city"        android:layout_width= "112DP"        android:layout_height= "Wrap_content"/>    <spinner        android:id= "@+id/spin_county"        android:layout_width= "100DP"        android:layout_ height= "Wrap_content"/></linearlayout>

Implementation code:

Package Com.example.android.demo.spinner;import Android.os.bundle;import Android.app.activity;import Android.view.view;import Android.widget.adapterview;import Android.widget.arrayadapter;import  Android.widget.spinner;public class Mainactivity extends activity{private Spinner provincespinner = null;     Provincial (province and municipality) private Spinner cityspinner = null;    Prefecture-level private Spinner Countyspinner = null;  County level (district, county, city) arrayadapter<string> provinceadapter = null;    Provincial adapter arrayadapter<string> cityadapter = null;    Ground level adapter arrayadapter<string> countyadapter = null;        County-level Adapter static int provinceposition = 3;    Provincial option value Private string[] province = new string[] {"Beijing", "Shanghai", "Tianjin", "Guangdong"};//, "Chongqing", "Heilongjiang", "Jiangsu", "Shandong", "Zhejiang", "Hong Kong", "Macau"}; Ground level option value private string[][] City = new string[][] {{"Dongcheng", "Xicheng", "Chongwen District", "Xuanwu District", "Chaoyang District", "Haidian", "Fengtai", "Shijingshan", "Mentougou", "Fangshan", "Tongzhou District", "Shunyi District", "Daxing District", "Changping District", "Pinggu District", "Huairou", "Miyun", "Yanqing County"}, {"Changning", "Jingan", "Putuo", "Zhabei", "Hongkou"}, { "Heping", "Hedong", "Hexi District", "Nankai District", "Hebei District", "Hongqiao District", "Tanggu District", "Hangu District", "Grand Port District", "Dongli District"}, {" Guangzhou "," Shenzhen "," Shaoguan "//," Zhuhai "," Shantou "," Foshan "," Zhanjiang "," Zhaoqing "," Jiangmen "," Maoming "," Huizhou "," Meizhou ",//" Shanwei "," Heyuan "," Yangjiang "," Qingyuan "," Dongguan "," Zhongshan "," Chaozhou "    , "Jieyang", "Yunfu"};                        County option value Private string[][][] County = new string[][][] {//Beijing {"None"},{"No"},{"None"},{"None"},{"None"},{"None"},{"None"},{"None"},{"None"},{"None"}, {"None"},{"None"},{"None"},{"None"},{"None"},{"None"},{                    "None"},{"no"}, {//Shanghai {"None"},{"None"},{"None"},{"None"},{"None"} }, {//Tianjin {"No"},{"no"},{"None"},{"None"},{"None"},{"None"},{"None"},{"None"},{"None"},{ "None"}, {//Guangdong {"Haizhu District", "Liwan District", "Yuexiu District"), "Baiyun District", "Luogang District", "Tianhe District", "Huangpu District", "Huadou District", "Conghua", "Add City", "Fanyu District", "Nansha"},//guangzhou {"Baoan District", "Futian District", "Longgang District", "Luohu", "Nanshan", "Yantian District"}            ,//Shenzhen {"Wujiang District", "Zhenzhou District", "Qujiang District", "Lechang", "Nanxiong", "Shixing County", "Renhua County", "Wengyuan County", "Xinfeng County", "Ruyuan County"}//Shaoguan}        };        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    Setspinner (); }/* * Set dropdown box */private void Setspinner () {Provincespinner = (Spinner) Findviewbyid (R.        Id.spin_province);        Cityspinner = (Spinner) Findviewbyid (r.id.spin_city);        Countyspinner = (Spinner) Findviewbyid (R.id.spin_county); Bind adapter and value Provinceadapter = new Arrayadapter<string> (Mainactivity.this, Android.        R.layout.simple_spinner_item, province);        Provincespinner.setadapter (Provinceadapter);  Provincespinner.setselection (3,true); Sets the default check, where the 4th value is checked by default CityadaPter = new Arrayadapter<string> (Mainactivity.this, Android.        R.layout.simple_spinner_item, city[3]);        Cityspinner.setadapter (Cityadapter);  Cityspinner.setselection (0,true); The No. 0 countyadapter = new arrayadapter<string> (Mainactivity.this, Android) is selected by default.        R.layout.simple_spinner_item, county[3][0]);        Countyspinner.setadapter (Countyadapter);                Countyspinner.setselection (0, true);  Provincial drop-down frame monitor provincespinner.setonitemselectedlistener (new Adapterview.onitemselectedlistener () {// Indicates that the option is changed to trigger this method, the main implementation: dynamically change the binding value of the ground level adapter @Override public void onitemselected (adapterview<?> arg0 , View arg1, int position, long Arg3) {//position is the ordinal of the current provincial selected value//change the value of the ground level adapter to city[ Position] Value Cityadapter = new Arrayadapter<string> (Mainactivity.this, Androi D.r.layout.simple_spinner_item, City[position]);                Set the option for Level two drop-down list of content adapter Cityspinner.setadapter (cityadapter);    Provinceposition = position; Record the current provincial serial number, leave the following to modify the county-level adapter when used} @Override public void onnothingselected (adapterview<?> a                rg0) {}}); Ground level dropdown monitor Cityspinner.setonitemselectedlistener (new Adapterview.onitemselectedlistener () {@Overri             De public void onitemselected (adapterview<?> arg0, View arg1, int position, long arg3) {countyadapter = new arrayadapter<string> (Mainactivity.this, Android Oid.                R.layout.simple_spinner_item, county[provinceposition][position]);            Countyspinner.setadapter (Countyadapter);        } @Override public void onnothingselected (Adapterview<?> arg0) {}    }); }}

To get the selected value in the drop-down box spinner, use this method to OK.

Provincespinner.getselecteditem (). toString ();

Cityspinner.getselecteditem (). toString ();

Countyspinner.getselecteditem (). toString ();


The END


Android implementation three-level linkage drop-down box drop-down list spinner instances

Related Article

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.