Android to achieve three-level linkage drop-down box Drop-down list spinner Instance code _android

Source: Internet
Author: User

Main implementation: Dynamic loading of all levels of the Drop-down value of the adapter

When listening to this level dropdown box, the binding value of the subordinate adapter is modified when the selected value of the dropdown box changes.

XML layout:

Copy Code code as follows:

<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:

Copy Code code as follows:

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; Cities
Private Spinner countyspinner = null; County level (district, county, city)
Arrayadapter<string> provinceadapter = null; Provincial adapters
Arrayadapter<string> cityadapter = null; Ground-level adapters
Arrayadapter<string> countyadapter = null; County Adapter
static int provinceposition = 3;


Provincial option values
Private string[] province = new string[] {"Beijing", "Shanghai", "Tianjin", "Guangdong"};//, "Chongqing", "Heilongjiang", "Jiangsu", "Shandong", "Zhejiang", "Hong Kong", "Macao"};
Ground-level option values
Private string[][] City = new string[][]
{
{"Dongcheng District", "Xicheng District", "Chongwen District", "Xuanwu District", "Chao Yang Qu", "Haidian District", "Fengtai District", "Shijingshan," and "Mentougou,"
"Fangshan District", "Tongzhou District", "Shunyi District", "Daxing District", "Changping District", "Pinggu District", "Huairou", "Miyun County",
"Yanqing County"},
{"Changning District", "Jingan", "Putuo", "Zhabei", "Hongkou"},
{"Zone of Peace", "Hedong District", "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
{"No"},{"},{" without "},{" without "},{" without "},{" without "},{" without "},{" without "},{" without "},{" "None"},
{"No"},{"no"},{"no"},{"no"},{"no"},{"no"},{"no"},{"none"}
},
{//Shanghai
{"No"},{"no"},{"no"},{"no"},{"none"}
},
{//Tianjin
{"No"},{"no"},{"no"},{"no"},{"no"},{"no"},{"no"},{"no"},{"no"},{"none"}
},
{//Guangdong
{"Haizhu District", "Liwan District", "Yuexiu District", "Baiyun District", "Luogang area", "Tianhe District", "Huangpu District", "Huadou District", "Conghua", "Add City", "Fanyu District", "Nansha"},//Guangzhou
{"Baoan District", "Futian District", "Longgang District", "Luohu District", "Nanshan District", "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 the 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);

Binding adapters and values
Provinceadapter = new Arrayadapter<string> (Mainactivity.this,
Android. R.layout.simple_spinner_item, province);
Provincespinner.setadapter (Provinceadapter);
Provincespinner.setselection (3,true); Set the default selection, where the 4th value is selected 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 One is selected by default

Countyadapter = new Arrayadapter<string> (Mainactivity.this,
Android. R.layout.simple_spinner_item, county[3][0]);
Countyspinner.setadapter (Countyadapter);
Countyspinner.setselection (0, true);


Provincial drop down box monitor
Provincespinner.setonitemselectedlistener (New Adapterview.onitemselectedlistener ()
{

This method is triggered when the option is changed: dynamically changing the binding value of the ground-level adapter
@Override
public void onitemselected (adapterview<?> arg0, View arg1, int position, long arg3)
{
Position the number of the value selected for the current province

Change the value of a ground-level adapter to a value in city[position]
Cityadapter = new Arrayadapter<string> (
Mainactivity.this, Android. R.layout.simple_spinner_item, city[position]);
Set the option content adapter for the level two Drop-down list
Cityspinner.setadapter (Cityadapter);
Provinceposition = position; Record the current provincial serial number, leaving the following to modify the county adapter
}

@Override
public void onnothingselected (adapterview<?> arg0)
{

}

});


Ground level drop down monitor
Cityspinner.setonitemselectedlistener (New Adapterview.onitemselectedlistener ()
{

@Override
public void onitemselected (adapterview<?> arg0, View arg1,
int position, long arg3)
{
Countyadapter = new Arrayadapter<string> (Mainactivity.this,
Android. R.layout.simple_spinner_item, county[provinceposition][position]);
Countyspinner.setadapter (Countyadapter);
}

@Override
public void onnothingselected (adapterview<?> arg0)
{

}
});
}
}


To get the value selected in the dropdown box spinner, use the following method to make it OK

Provincespinner.getselecteditem (). toString ();

Cityspinner.getselecteditem (). toString ();

Countyspinner.getselecteditem (). toString ();


The end

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.