[Advanced Spinner Features of Android Components--Cascade Selection]

1. Cascading operation of provinces and cities

The select drop-down list of the Web system can realize the cascade operation of provinces and cities, and the cities under the province will appear in the selected province, rather than cities in other provinces, as shown in the following figure:


 So can the drop-down list component Spinner in the Android system achieve provincial and urban cascading? The answer is yes, as shown below:



 

 2. The source code of Android provincial and urban cascading is as follows:

1) Interface file, refer to

              

Permalink:  http://gaojingsong.iteye.com/blog/2356364

Preview article: [Use of Spinner for Android Components]

                <TextView android:layout_width="fill_parent"

                      android:layout_height="wrap_content" android:text="城市:" />

               <Spinner android:id="@+id/sp" android:layout_width="wrap_content"

                     android:layout_height="wrap_content" android:entries="@array/my_citys"

                     android:prompt="@string/info" />

             <TextView android:layout_width="fill_parent"

                    android:layout_height="wrap_content" android:text="区域:" />

            <Spinner android:id="@+id/areas" android:layout_width="wrap_content"

                  android:layout_height="wrap_content" android:entries="@array/my_areas"

                  android:prompt="@string/info" />

2) The cascaded JAVA code is as follows:

         //Define secondary list array

        private String[][] areas = new String[][]{{"Haidian","Chaoyang"},{"Pudong"},{"Guangzhou","Dongguan","Shenzhen"}};

        Spinner  areasSpinner = (Spinner) this.findViewById(R.id.areas);

        Spinner  spCity = (Spinner) this.findViewById(R.id.sp);

        //Register listener events

        spCity.setOnItemSelectedListener(new MyCityItemSelectedListener());

 

 

Monitor core code

       private class MyCityItemSelectedListener implements OnItemSelectedListener{

       /**

           parent The AdapterView where the selection happened

          view  The view within the AdapterView that was clicked

          position  The position of the view in the adapter

          id    The row id of the item that is selected

       */

     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

         ArrayAdapter adapter = new ArrayAdapter<String>(HelloActivity.this, 

         android.R.layout.simple_spinner_item, HelloActivity.this.areas[position]);

         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        areasSpinner.setAdapter(adapter);

      }

 

       public void onNothingSelected(AdapterView<?> arg0) {

       }

     }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326427737&siteId=291194637