码迷,mamicode.com
首页 > 其他好文 > 详细

使用pull解析和spinner实现省市地区三级联动的效果

时间:2015-04-23 00:04:22      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:解析   级联   

技术分享

xml资源文件

<?xml version="1.0" encoding="UTF-8"?>
    <!--
        START_DOCUMET:第一个节点 END_DOCUMET:尾节点 START_TAG:其他的首部的节点 p pn c cn...
        END_TAG:其他的尾部的节点 </p> </c> ...
    -->
<citylist>
    <p p_id="01">
        <pn>北京</pn>
        <c c_id="0101">
            <cn>北京</cn>
            <d d_id="101010101">北京</d>
            <d d_id="101010200">海淀</d>
            <d d_id="101010300">朝阳</d>
            <d d_id="101010400">顺义</d>
            <d d_id="101010500">怀柔</d>
            <d d_id="101010600">通州</d>
            <d d_id="101010700">昌平</d>
            <d d_id="101010800">延庆</d>
            <d d_id="101010900">丰台</d>
            <d d_id="101011000">石景山</d>
            <d d_id="101011100">大兴</d>
            <d d_id="101011200">房山</d>
            <d d_id="101011300">密云</d>
            <d d_id="101011400">门头沟</d>
            <d d_id="101011500">平谷</d>
        </c>
    </p>

xml文件只选取了部分,完整文件包括中国的所有省市地区。

布局文件

<RelativeLayout 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"
    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=".MainActivity" >

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


        />

    <Spinner 
        android:id="@+id/spinner02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/spinner01"
        />

    <Spinner 
        android:id="@+id/spinner03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/spinner02"
        />

</RelativeLayout>

城市类

package com.example.sjliandong;



import java.util.ArrayList;
import java.util.List;

public class City {
    private String id;
    private String name;
    private List<District> districts;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }


    public List<District> getDistricts() {
        return districts;
    }
    public void setDistricts(List<District> districts) {
        this.districts = districts;
    }
    @Override
    public String toString() {
        return name;
    }


}

地区类

package com.example.sjliandong;

public class District {
    private String id;
    private String name;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return name;
    }


}

省类

package com.example.sjliandong;


import java.util.*;

public class Provence {
    private String id;
    private String name;
    private List<City> citys;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public List<City> getCitys() {
        return citys;
    }
    public void setCitys(List<City> citys) {
        this.citys = citys;
    }
    @Override
    public String toString() {
        return name;
    }


}

Activity类

package com.example.sjliandong;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.Resources;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends Activity {
    private List<Provence> provences;
    private Provence provence;
    ArrayAdapter<Provence> adapter01;
    ArrayAdapter<City> adapter02;
    ArrayAdapter<District> adapter03;
    private Spinner spinner01, spinner02, spinner03;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spinner01 = (Spinner) findViewById(R.id.spinner01);
        spinner02 = (Spinner) findViewById(R.id.spinner02);
        spinner03 = (Spinner) findViewById(R.id.spinner03);

        try {
            provences = getProvinces();
        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        adapter01 = new ArrayAdapter<Provence>(this,
                android.R.layout.simple_list_item_1, provences);
        spinner01.setAdapter(adapter01);
        spinner01.setSelection(0, true);

        adapter02 = new ArrayAdapter<City>(this,
                android.R.layout.simple_list_item_1, provences.get(0)
                        .getCitys());
        spinner02.setAdapter(adapter02);
        spinner02.setSelection(0, true);

        adapter03 = new ArrayAdapter<District>(this,
                android.R.layout.simple_list_item_1, provences.get(0)
                        .getCitys().get(0).getDistricts());
        spinner03.setAdapter(adapter03);
        spinner03.setSelection(0, true);

        spinner01.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                provence = provences.get(position);
                adapter02 = new ArrayAdapter<City>(MainActivity.this,
                        android.R.layout.simple_list_item_1, provences.get(
                                position).getCitys());
                spinner02.setAdapter(adapter02);

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


        spinner02.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                adapter03 = new ArrayAdapter<District>(MainActivity.this,
                        android.R.layout.simple_list_item_1, provence.getCitys().get(position)
                        .getDistricts());
                spinner03.setAdapter(adapter03);

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub

            }
        });
    }

    public List<Provence> getProvinces() throws XmlPullParserException,
            IOException {
        List<Provence> provinces = null;
        Provence province = null;
        List<City> citys = null;
        City city = null;
        List<District> districts = null;
        District district = null;
        Resources resources = getResources();

        InputStream in = resources.openRawResource(R.raw.citys_weather);

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        XmlPullParser parser = factory.newPullParser();

        parser.setInput(in, "utf-8");
        int event = parser.getEventType();
        while (event != XmlPullParser.END_DOCUMENT) {
            switch (event) {
            case XmlPullParser.START_DOCUMENT:
                provinces = new ArrayList<Provence>();
                break;
            case XmlPullParser.START_TAG:
                String tagName = parser.getName();
                if ("p".equals(tagName)) {
                    province = new Provence();
                    citys = new ArrayList<City>();
                    int count = parser.getAttributeCount();
                    for (int i = 0; i < count; i++) {
                        String attrName = parser.getAttributeName(i);
                        String attrValue = parser.getAttributeValue(i);
                        if ("p_id".equals(attrName))
                            province.setId(attrValue);
                    }
                }
                if ("pn".equals(tagName)) {
                    province.setName(parser.nextText());
                }
                if ("c".equals(tagName)) {
                    city = new City();
                    districts = new ArrayList<District>();
                    int count = parser.getAttributeCount();
                    for (int i = 0; i < count; i++) {
                        String attrName = parser.getAttributeName(i);
                        String attrValue = parser.getAttributeValue(i);
                        if ("c_id".equals(attrName))
                            city.setId(attrValue);
                    }
                }
                if ("cn".equals(tagName)) {
                    city.setName(parser.nextText());
                }
                if ("d".equals(tagName)) {
                    district = new District();
                    int count = parser.getAttributeCount();
                    for (int i = 0; i < count; i++) {
                        String attrName = parser.getAttributeName(i);
                        String attrValue = parser.getAttributeValue(i);
                        if ("d_id".equals(attrName))
                            district.setId(attrValue);
                    }
                    district.setName(parser.nextText());
                    districts.add(district);
                }
                break;
            case XmlPullParser.END_TAG:
                if ("c".equals(parser.getName())) {
                    city.setDistricts(districts);
                    citys.add(city);
                }
                if ("p".equals(parser.getName())) {
                    province.setCitys(citys);
                    provinces.add(province);
                }

                break;

            }
            event = parser.next();

        }
        return provinces;
    }
}

效果:
技术分享
技术分享
技术分享
完整项目下载地址:github spinner小练习

使用pull解析和spinner实现省市地区三级联动的效果

标签:解析   级联   

原文地址:http://blog.csdn.net/a910626/article/details/45204143

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!