基于Spinner的省市县三级联动选择显示

通过xml解析获得数据源,通过Spinner进行三级的联动显示,以下为源代码

Mainactivity代码

public class MainActivity extends Activity {

    Spinner spinner1, spinner2, spinner3;
    Province province = null;
    City city = null;
    List list = new ArrayList();
    List list1 = new ArrayList();// 省的名字列表
    List list2 = new ArrayList();// 城市名字列表
    List list3 = new ArrayList();// 地区名字列表

    int index1;// 选取的省
    int index2;// 选取的市

    ArrayAdapter arrayAdapter1;
    ArrayAdapter arrayAdapter2;
    ArrayAdapter arrayAdapter3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 初始化spinner
        spinner2 = (Spinner) findViewById(R.id.s2);
        spinner1 = (Spinner) findViewById(R.id.s1);
        spinner3 = (Spinner) findViewById(R.id.s3);
        parser1();// 通过parse1函数,赋值list
        final int layOutId = android.R.layout.simple_list_item_1;
        // 获得省会的list
        for (Province tp : list) {
            list1.add(tp.getName());
        }

        arrayAdapter1 = new ArrayAdapter(this,
                layOutId, list1);
        // 初始化list2,用于初始化城市适配器
        for (City tc : list.get(0).getCitys()) {
            list2.add(tc.getName());
        }
        arrayAdapter2 = new ArrayAdapter(this,
                layOutId, list2);
        // 初始化list3,用于初始化地区适配器
        for (String ts : list.get(0).getCitys().get(0).getDistricts()) {
            list3.add(ts);
        }
        arrayAdapter3 = new ArrayAdapter(this,
                layOutId, list3);
        spinner1.setAdapter(arrayAdapter1);
        spinner1.setSelection(0, true);
        spinner2.setAdapter(arrayAdapter2);
        spinner2.setSelection(0, true);
        spinner3.setAdapter(arrayAdapter3);
        spinner3.setSelection(0, true);

        spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView parent, View view,
                    int position, long id) {
                index1 = position;
                //对两个适配器清零
                arrayAdapter2.clear();
                arrayAdapter3.clear();
                // 获取点击的省份
                province = list.get(index1);
                // 遍历制作城市list2
                for (City tc : province.getCitys()) {
                    list2.add(tc.getName());
                }
                //遍历制作地区list3
                for (String ts : province.getCitys().get(0).getDistricts()) {
                    list3.add(ts);
                }
                // 对2号适配器重新赋值
                arrayAdapter2 = new ArrayAdapter(MainActivity.this,
                        layOutId,list2);
                spinner2.setAdapter(arrayAdapter2);
                arrayAdapter3 = new ArrayAdapter(MainActivity.this,
                        layOutId, list3);
                spinner3.setAdapter(arrayAdapter3);
            }

            @Override
            public void onNothingSelected(AdapterView parent) {

            }

        });
        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView parent, View view,
                    int position, long id) {
                arrayAdapter3.clear();
                index2 = position;
                // 获取点击的城市
                city = list.get(index1).getCitys().get(index2);
                // 遍历制作地区list3
                for (String ts : city.getDistricts()) {
                    list3.add(ts);
                }
                // 对3号适配器重新赋值
                arrayAdapter3 = new ArrayAdapter(MainActivity.this,
                        layOutId, list3);
                spinner3.setAdapter(arrayAdapter3);
            }

            @Override
            public void onNothingSelected(AdapterView parent) {

            }
        });
    }

    public void parser1() {

        // 创建解析器,并制定解析的xml文件
        XmlResourceParser parser = getResources().getXml(R.xml.citys_weather);
        try {
            int type = parser.getEventType();
            while (type != XmlResourceParser.END_DOCUMENT) {
                String tag = parser.getName();// 获得标签名
                switch (type) {
                case XmlResourceParser.START_DOCUMENT:
                    list = new ArrayList();
                    break;
                case XmlResourceParser.START_TAG:
                    switch (tag) {
                    case "pn":
                        province = new Province();
                        province.setName(parser.nextText());
                        break;
                    case "cn":
                        city = new City();
                        city.setName(parser.nextText());
                        break;
                    case "d":
                        city.getDistricts().add(parser.nextText());
                        break;
                    default:
                        break;
                    }
                    break;

                case XmlResourceParser.END_TAG:

                    if ("c".equals(tag)) {
                        province.getCitys().add(city);
                    }
                    if ("p".equals(tag)) {
                        list.add(province);
                    }
                    break;
                default:
                    break;
                }
                type = parser.next();
            }
        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        /*
         * catch (FileNotFoundException e) { // TODO Auto-generated catch block
         * e.printStackTrace(); }
         */
        catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

province代码


public class Province {// 省份内
    private String name;

    private List citys;


    public Province() {
        citys = new ArrayList();
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public List getCitys() {
        return citys;
    }


    public void setCitys(List citys) {
        this.citys = citys;
    }


}

City代码

public class City {

    private String name;

    private List districts;

    public City(){
        districts = new ArrayList();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List getDistricts() {
        return districts;
    }

    public void setDistricts(List districts) {
        this.districts = districts;
    }

}

布局文件

<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="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <Spinner
        android:id="@+id/s1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:spinnerMode="dropdown" >
    Spinner>

    <Spinner
        android:id="@+id/s2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:spinnerMode="dropdown" >
    Spinner>

    <Spinner
        android:id="@+id/s3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:spinnerMode="dropdown" >
    Spinner>

LinearLayout>

你可能感兴趣的:(Android,Spinner,界面逻辑)