Android--spinner control to read XML resources, provincial and municipal level two interaction

Source: Internet
Author: User

(1) First create a provincial and municipal Arrays.xml folder in the values below the Res folder, as follows

<?XML version= "1.0" encoding= "Utf-8"?><Resources><String-arrayname= "Province">       <Item>Provinces</Item> <Item>Hebei Province</Item> <Item>Shanxi Province</Item>     <Item>Beijing</Item> </String-array> <String-arrayname= "HB">       <Item>City</Item> <Item>Chengde</Item> <Item>Handan</Item>     <Item>Langfang city</Item> </String-array> <String-arrayname= "BJ">        <Item>City</Item> <Item>Haidian district</Item> <Item>Chaoyang</Item>     <Item>Chongwen district</Item> </String-array> <String-arrayname= "Shx">      <Item>City</Item> <Item>Datong city</Item> <Item>Linfen city</Item> </String-array> </Resources>

(2) then add two spinner controls to the layout file, and the XML layout code is as follows

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:id= "@+id/linearlayout1"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.example.spinner.MainActivity" >    <SpinnerAndroid:id= "@+id/sppro"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" />    <SpinnerAndroid:id= "@+id/spcity"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" /></LinearLayout>

(3) Here the resources and layout interface has been set up, the next is in the Mainactivity with code to control the linkage between the provinces and cities, in the comments in the code, the code is as follows

public class Mainactivity extends Activity {private Spinner sppro, spcity; @Overrideprotected void OnCreate (Bundle Savedin Stancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); SpPro = (Spinner) Findviewbyid (r.id.sppro); spcity = (Spinner) Findviewbyid (r.id.spcity);//Set Data source arrayadapter<charsequence> adapter = Arrayadapter.createfromresource (this, r.array.province,android. R.layout.simple_spinner_item);//Set the dropdown list style Adapter.setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item);//Bind the data source to the spinner control Sppro.setadapter (adapter);// Spinner Option Select Event Sppro.setonitemselectedlistener (new Onitemselectedlistener () {@Overridepublic void onitemselected ( Adapterview<?> Parent, view view, int position, long ID) {//the parent here refers to the Sppro drop-down box spinner spinner = (spinner) parent; Use the Getitematposition (position) method to find the name of the province item String Pro = (string) spinner.getitematposition (position);// Set the city-level data adapter arrayadapter<charsequence> Cityadapter = Arrayadapter.createfromresourCE (Mainactivity.this, r.array.bj,android. R.layout.simple_spinner_item);//judgment, then set the adapter if (Pro.equals ("Hebei province")) {Cityadapter = Arrayadapter.createfromresource ( Mainactivity.this, R.array.hb,android. R.layout.simple_spinner_item);} else if (pro.equals ("Beijing")) {Cityadapter = Arrayadapter.createfromresource (Mainactivity.this, r.array.bj,android. R.layout.simple_spinner_item);} else if (pro.equals ("Shanxi Province")) {Cityadapter = Arrayadapter.createfromresource (Mainactivity.this, r.array.shx,android. R.layout.simple_spinner_item);} Spcity.setadapter (Cityadapter);} @Overridepublic void onnothingselected (adapterview<?> parent) {}});}}

The effect of the last run is as shown

Android--spinner control to read XML resources, provincial and municipal level two interaction

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.