码迷,mamicode.com
首页 > 移动开发 > 详细

android省市区三级联动案例:(三)主要功能实现

时间:2015-07-30 21:08:51      阅读:782      评论:0      收藏:0      [点我收藏+]

标签:

MainActivity.java

技术分享
package com.test.address;

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 com.test.address.adapter.AreaAdapter;
import com.test.address.adapter.CityAdapter;
import com.test.address.adapter.ProvinceAdapter;
import com.test.address.bean.Area;
import com.test.address.bean.City;
import com.test.address.bean.Province;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private static final String LOG_TAG = "MainActivity";
    private List<Province> provinceList;
    private TextView tvProvice, tvCity, tvArea;
    private Button btnSure;
    private int curProPosition = 0, curCityPosition = 0;// 当前省份,城市
    private boolean isGetCity = true, isGetArea = true;// 某省份下是否还有城市、地区
    private Context context;
    
    private final static int CHOICE_TYPE_PROVINCE = 1; //选择省份
    private final static int CHOICE_TYPE_CITY = 2; //选择城市
    private final static int CHOICE_TYPE_AREA = 3; //选择区域
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        context = MainActivity.this;

        // 初始化区域数据
        initAddressData();

        // 初始化view数据
        initView();

    }

    private void initView() {
        tvProvice = (TextView) findViewById(R.id.tv_province);
        tvCity = (TextView) findViewById(R.id.tv_city);
        tvArea = (TextView) findViewById(R.id.tv_area);
        btnSure = (Button) findViewById(R.id.btnSure);

        
        RelativeLayout ly_province = (RelativeLayout)findViewById(R.id.ly_province);
        RelativeLayout ly_city = (RelativeLayout)findViewById(R.id.ly_city);
        RelativeLayout ly_area = (RelativeLayout)findViewById(R.id.ly_area);
        
        ly_province.setOnClickListener(addressOnClick);
        ly_city.setOnClickListener(addressOnClick);
        ly_area.setOnClickListener(addressOnClick);
        btnSure.setOnClickListener(addressOnClick);
    }

    View.OnClickListener addressOnClick = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.ly_province:
                showAddressChoiceDialog(CHOICE_TYPE_PROVINCE);
                break;
                
            case R.id.ly_city: 
                String provice = tvProvice.getText().toString();
                if(provice==null || "".equals(provice.trim())){
                    Toast.makeText(context, "请先选择省份", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (isGetCity) {
                    showAddressChoiceDialog(CHOICE_TYPE_CITY);
                }

                break;
                
            case R.id.ly_area:
                String city = tvProvice.getText().toString();
                if(city==null || "".equals(city.trim())){
                    Toast.makeText(context, "请先选择城市", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (isGetArea) {
                    showAddressChoiceDialog(CHOICE_TYPE_AREA);
                }
                break;
            case R.id.btnSure:
                String result = tvProvice.getText().toString()+tvCity.getText().toString()+tvArea.getText().toString();
                Toast.makeText(context, result, Toast.LENGTH_SHORT).show();
                break;
                
            default:
                break;
            }

        }
    };

    /**
     * 显示地区选择对话框 type:1=省份,2=城市,3=地区
     * 
     * @param type
     */
    private void showAddressChoiceDialog(final int type) {
        final Dialog dialog = new Dialog(this);
        ListView listview = new ListView(this);
        listview.setBackgroundColor(Color.WHITE);

        if (type == CHOICE_TYPE_PROVINCE) {
            ProvinceAdapter proAdapter = new ProvinceAdapter(context, provinceList);
            listview.setAdapter(proAdapter);
            dialog.setTitle("请选择省份");
        } else if (type == CHOICE_TYPE_CITY) {
            CityAdapter cityAdpater = new CityAdapter(context, provinceList.get(curProPosition).getCityList());
            listview.setAdapter(cityAdpater);
            dialog.setTitle("请选择城市");
        } else if (type == CHOICE_TYPE_AREA) {
            AreaAdapter areaAdapter = new AreaAdapter(context,
                    provinceList.get(curProPosition).getCityList().get(curCityPosition).getAreaList());
            listview.setAdapter(areaAdapter);
            dialog.setTitle("请选择地区");
        }

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

                if (type == CHOICE_TYPE_PROVINCE) {
                    // 修改当前省份
                    curProPosition = position;
                    tvProvice.setText(provinceList.get(position).getProvince());
                    
                    // 判断是否有城市
                    if (provinceList.get(position).getCityList().size() < 1) {
                        tvCity.setText("");
                        tvArea.setText("");
                        isGetCity = false;
                        isGetArea = false;
                    } else {
                        // 城市设置默认为第一个
                        tvCity.setText(provinceList.get(position).getCityList().get(0).getCity());
                        curCityPosition = 0;
                        isGetCity = true;

                        // 判断是否有地区
                        if (provinceList.get(position).getCityList().get(0).getAreaList().size() < 0) {
                            tvArea.setText("");
                            isGetArea = false;
                        } else {
                            tvArea.setText(
                                    provinceList.get(position).getCityList().get(0).getAreaList().get(0).getArea());
                            isGetArea = true;
                            
                        }

                    }

                //如果选择是的是城市
                } else if (type == CHOICE_TYPE_CITY) {
                    
                    curCityPosition = position;
                    // 判断是否有地区
                    if (provinceList.get(curProPosition).getCityList().get(position).getAreaList().size() < 0) {
                        tvArea.setText("");
                        isGetArea = false;
                    } else {
                        tvArea.setText(
                                provinceList.get(curProPosition).getCityList().get(position).getAreaList().get(0).getArea());
                        isGetArea = true;
                        
                    }
                    
                    //如果选择是的是城区
                } else if (type ==CHOICE_TYPE_AREA) {
                    tvArea.setText(
                            provinceList.get(curProPosition).getCityList().get(curCityPosition).getAreaList().get(position).getArea());
                }

                dialog.dismiss();

                
            }
        }); //end of onItemClick

        dialog.setContentView(listview);
        dialog.show();
    }
    
    
    
    /**
     * 读取xml数据
     */
    private void initAddressData() {

        try {
            InputStream inputStream = getResources().openRawResource(R.raw.address);

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser parser = factory.newPullParser();
            parser.setInput(inputStream, "UTF-8");

            String province, city, area;
            Province proBean = null;
            City cityBean = null;
            Area areaBean = null;

            List<City> cityList = null;
            List<Area> areaList = null;

            int type = parser.getEventType();
            while (type != XmlPullParser.END_DOCUMENT) {
                String typeName = parser.getName();
                if (type == XmlPullParser.START_TAG) {
                    if ("root".equals(typeName)) {
                        provinceList = new ArrayList<Province>();
                    } else if ("province".equals(typeName)) {
                        province = parser.getAttributeValue(0);
                        proBean = new Province();
                        proBean.setProvince(province);
                        cityList = new ArrayList<City>();

                    } else if ("city".equals(typeName)) {
                        city = parser.getAttributeValue(0);
                        cityBean = new City();
                        cityBean.setCity(city);
                        areaList = new ArrayList<Area>();
                    } else if ("area".equals(typeName)) {
                        area = parser.getAttributeValue(0);
                        areaBean = new Area();
                        areaBean.setArea(area);
                    }

                } // end of if (type == XmlPullParser.START_TAG) {

                if (type == XmlPullParser.END_TAG) {
                    if ("root".equals(typeName)) {
                        // nothing
                    } else if ("province".equals(typeName)) {
                        provinceList.add(proBean);
                    } else if ("city".equals(typeName)) {
                        cityList.add(cityBean);
                        proBean.setCityList(cityList);
                    } else if ("area".equals(typeName)) {
                        areaList.add(areaBean);
                        cityBean.setAreaList(areaList);
                    }
                }

                type = parser.next();
            }

        } catch (XmlPullParserException e) {
            Log.i(LOG_TAG, Log.getStackTraceString(e));
        } catch (IOException e) {
            Log.i(LOG_TAG, Log.getStackTraceString(e));
        }

        Log.i(LOG_TAG, "provinceList size:" + provinceList.size());

    }

}
View Code

 

ProvinceAdapter.java

技术分享
package com.test.address.adapter;

import java.util.List;

import com.test.address.R;
import com.test.address.bean.Province;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ProvinceAdapter extends android.widget.BaseAdapter {

    private Context context;
    private List<Province> list;

    public ProvinceAdapter(Context context, List<Province> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TextView view = new TextView(context);
        // view.setPadding(10, 10,10, 10);
        // view.setText(prolist.get(position).getProvince());
        View view = convertView;
        TextView tvName = null;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.address_info, null);
            tvName = (TextView) view.findViewById(R.id.tv_address_name);
            view.setTag(tvName);
        } else {
            tvName = (TextView) view.getTag();
        }

        tvName.setText(list.get(position).getProvince());

        return view;
    }

}
View Code

 

cityAdapter.java

技术分享
package com.test.address.adapter;

import java.util.List;

import com.test.address.R;
import com.test.address.bean.City;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class CityAdapter extends android.widget.BaseAdapter {

    private Context context;
    private List<City> list;

    public CityAdapter(Context context, List<City> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TextView view = new TextView(context);
        // view.setPadding(10, 10,10, 10);
        // view.setText(prolist.get(position).getProvince());
        View view = convertView;
        TextView tvName = null;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.address_info, null);
            tvName = (TextView) view.findViewById(R.id.tv_address_name);
            view.setTag(tvName);
        } else {
            tvName = (TextView) view.getTag();
        }

        tvName.setText(list.get(position).getCity());

        return view;
    }

}
View Code

 

AreaAdapter

技术分享
package com.test.address.adapter;

import java.util.List;

import com.test.address.R;
import com.test.address.bean.Area;
import com.test.address.bean.Province;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class AreaAdapter extends android.widget.BaseAdapter {

    private Context context;
    private List<Area> list;

    public AreaAdapter(Context context, List<Area> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TextView view = new TextView(context);
        // view.setPadding(10, 10,10, 10);
        // view.setText(prolist.get(position).getProvince());
        View view = convertView;
        TextView tvName = null;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.address_info, null);
            tvName = (TextView) view.findViewById(R.id.tv_address_name);
            view.setTag(tvName);
        } else {
            tvName = (TextView) view.getTag();
        }

        tvName.setText(list.get(position).getArea());

        return view;
    }

}
View Code

 

ProvinceBean.java

技术分享
package com.test.address.bean;

import java.util.List;

public class Province {
    
    private String province;
    private List<City> cityList;
    public String getProvince() {
        return province;
    }
    public void setProvince(String province) {
        this.province = province;
    }
    public List<City> getCityList() {
        return cityList;
    }
    public void setCityList(List<City> cityList) {
        this.cityList = cityList;
    }
    
}
View Code

 

AreaBean.java

技术分享
package com.test.address.bean;

public class Area {

    private String area;

    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        this.area = area;
    }
    
    
}
View Code

 

CityBean.java

技术分享
package com.test.address.bean;

import java.util.List;

public class City {

    private String city;
    private List<Area> areaList;

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public List<Area> getAreaList() {
        return areaList;
    }

    public void setAreaList(List<Area> areaList) {
        this.areaList = areaList;
    }

}
View Code

 

android省市区三级联动案例:(三)主要功能实现

标签:

原文地址:http://www.cnblogs.com/2015android/p/4690337.html

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