Android之省市县三级联动

layout布局




                
                    
                    
                        
                        
                        
                        
                        
                        
                    

                
             







java代码:

package com.example.login;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import java.io.*;
import org.xmlpull.v1.XmlPullParserException;
import java.text.*;
import java.util.*;

public class LoginActivity extends AppCompatActivity {
    private Spinner sp_province,sp_city,sp_county;
    
    private ArrayAdapter provinceArrayAdapter;
    private ArrayAdapter cityArrayAdapter;
    private ArrayAdapter countyArrayAdapter;
    private List provinces;
    private int prov_position;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_main);

        Intent intent=getIntent();

        //得到容器视图
        sp_city=(Spinner)findViewById(R.id.city);
        sp_county=(Spinner)findViewById(R.id.county);
        sp_province=(Spinner)findViewById(R.id.province);


        //得到解析后的数据
        InputStream is = getResources().openRawResource(R.raw.locations);
        try { provinces= LocationService.getProvince(is);
        }catch (XmlPullParserException |IOException e)
        { e.printStackTrace(); }
        //设置省的适配器
        provinceArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, provinces);
        sp_province.setAdapter(provinceArrayAdapter);
        sp_province.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView parent, View view, int position, long id) {
                prov_position = position;
                //设置市的适配器
               cityArrayAdapter = new ArrayAdapter(LoginActivity.this, android.R.layout.simple_spinner_item, provinces.get(position).getCitys());
               sp_city.setAdapter(cityArrayAdapter);

            }

            @Override
            public void onNothingSelected(AdapterView parent) {

            }
        });
        sp_city.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView parent, View view,
                                       int position, long id)
            {
                //设置地区的适配器
                countyArrayAdapter= new ArrayAdapter(LoginActivity.this, android.R.layout.simple_spinner_item, provinces.get(prov_position).getCitys().get(position).getCounties());
                sp_county.setAdapter(countyArrayAdapter);
            }
            @Override
            public void onNothingSelected(AdapterView parent)
            {
            }
        });

 
	}

}

city.java

package com.example.login;

import java.util.List;

public class City {
    private String  id;
    private  String name;
    private List counties;

    public City() {
        super();
    }
    public City(String id, String name, List counties) {
        super();
        this.id = id;
        this.name = name;
        this.counties = counties;
    }

    public String getName() {
        return name;
    }

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

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List getCounties() {
        return counties;
    }

    public void setCounties(List counties) {
        this.counties = counties;
    }
    public String toString() {
        return name;
    }
}

County .java

package com.example.login;

public class County {
    private String  id;
    private  String name;

    public County() {
        super();
    }

    public County(String id, String name) {
        super();
        this.id = id;
        this.name=name;
    }

    public String getName() {
        return name;
    }

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

    public String toString() {
        return name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

Province .java

package com.example.login;


import java.util.List;

public class Province {
    private String  id;
    private  String name;
    private List citys;
    public  Province()
    {
        super();
    }
    public Province(String id,String name,List citys)
    {
        super();
        this.name=name;
        this.id=id;
        this.citys=citys;
    }

    @Override
    public String toString() {
        return name;
    }

    public String getName() {
        return name;
    }

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

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List getCitys() {
        return citys;
    }

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

LocationService .java

package com.example.login;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

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

public class LocationService {
    public static List getProvince(InputStream is) throws XmlPullParserException, IOException {
        Listprovinces =null;
        Province province=null;
        List cities=null;
        City city=null;
        List counties=null;
        County county=null;

        String tagName=null;

        //初始化解析器
        XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
        XmlPullParser parser=factory.newPullParser();

        parser.setInput(is,"utf-8");

        int event = parser.getEventType();
        while(event!=XmlPullParser.END_DOCUMENT)
        {
            switch (event)
            {
                case XmlPullParser.START_TAG:
                    tagName=parser.getName();
                    if ("citylist".equals(tagName))
                    {
                        provinces = new ArrayList();
                    }
                    else if("p".equals(tagName))
                    {
                        province=new Province();
                        cities = new ArrayList();
                        int count =parser.getAttributeCount();
                        for(int i=0;i();
                        int count =parser.getAttributeCount();
                        for(int i=0;i

你可能感兴趣的:(Android)