AddressAlertDialog.java 12 KB
package com.fish.drp.div;

import android.app.Dialog;
import android.content.Context;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;


import com.drp.R;
import com.fish.drp.div.util.Address;
import com.fish.drp.div.util.AddressManager;
import com.fish.drp.div.util.ChineseData;
import com.fish.drp.div.util.UIUtil;
import com.fish.drp.div.wheel.CityModel;
import com.fish.drp.div.wheel.DistrictModel;
import com.fish.drp.div.wheel.ProvinceModel;
import com.fish.drp.div.wheel.XmlParserHandler;
import com.fish.drp.div.wheel.widget.OnWheelChangedListener;
import com.fish.drp.div.wheel.widget.WheelView;
import com.fish.drp.div.wheel.widget.adapters.ArrayWheelAdapter;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class AddressAlertDialog extends Dialog implements
        OnWheelChangedListener {

    private Button btNegative, btPositive;
    /**
     * 所有省
     */
    protected String[] mProvinceDatas;
    /**
     * key - 省 value - 市
     */
    protected Map<String, String[]> mCitisDatasMap = new HashMap<String, String[]>();
    /**
     * key - 市 values - 区
     */
    protected Map<String, String[]> mDistrictDatasMap = new HashMap<String, String[]>();

    /**
     * key - 区 values - 邮编
     */
//    protected Map<String, String> mZipcodeDatasMap = new HashMap<String, String>();

    /**
     * 当前省的名称
     */
    protected String mCurrentProviceName;
    /**
     * 当前市的名称
     */
    protected String mCurrentCityName;
    /**
     * 当前区的名称
     */
    protected String mCurrentDistrictName = "";
    private WheelView mViewProvince;
    private WheelView mViewCity;
    private WheelView mViewDistrict;
    private Context mContext;
    String sheng, shi, qu;
    private View view;

    public AddressAlertDialog(Context context) {
        super(context, R.style.commonDialog);
        mContext = context;
    }

    public AddressAlertDialog(Context context, String pro, String shi, String qu, View address) {
        super(context, R.style.commonDialog);
        mContext = context;
        this.sheng = pro;
        this.shi = shi;
        this.qu = qu;
        view = address;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_address_pop);
        setCanceledOnTouchOutside(true);
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();
        lp.width = UIUtil.getScreenWidth(getContext());
        lp.gravity = Gravity.BOTTOM;
        lp.dimAmount = 0.6f;
        this.getWindow().setAttributes(lp);
        this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        btNegative = (Button) findViewById(R.id.cancel);
        btPositive = (Button) findViewById(R.id.submit);

        btNegative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        btPositive.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                sheng = mCurrentProviceName;
                shi = mCurrentCityName;
                qu = mCurrentDistrictName;
                ((TextView) view).setText(mCurrentProviceName + mCurrentCityName + mCurrentDistrictName);
                ((TextView) view).setTag(mCurrentProviceName + ";" + mCurrentCityName + ";" + mCurrentDistrictName);
                dismiss();
            }
        });

        mViewProvince = (WheelView) findViewById(R.id.id_province);
        mViewCity = (WheelView) findViewById(R.id.id_city);
        mViewDistrict = (WheelView) findViewById(R.id.id_district);
        mViewProvince.addChangingListener(this);
        mViewCity.addChangingListener(this);
        mViewDistrict.addChangingListener(this);
//        initProvinceDatas();
        getProvinceDatas();
        setUpData();

    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (wheel.getId() == R.id.id_province) {
            updateCities();
        } else if (wheel.getId() == R.id.id_city) {
            updateAreas();
        } else if (wheel.getId() == R.id.id_district) {
            mCurrentDistrictName = mDistrictDatasMap.get(mCurrentCityName)[mViewDistrict.getCurrentItem()];
        }


    }

    private void setUpData() {
//        initProvinceDatas();
//        getProvinceDatas();
        mViewProvince.setViewAdapter(new ArrayWheelAdapter<String>(mContext, mProvinceDatas));
        // 设置可见条目数量
        mViewProvince.setVisibleItems(7);
        mViewCity.setVisibleItems(7);
        mViewDistrict.setVisibleItems(7);
        updateCities();
//        updateAreas();
    }

    private void updateCities() {
        int pCurrent = mViewProvince.getCurrentItem();
        mCurrentProviceName = mProvinceDatas[pCurrent];
        String[] cities = mCitisDatasMap.get(mCurrentProviceName);
        if (cities == null) {
            cities = new String[]{""};
        }
        mViewCity.setViewAdapter(new ArrayWheelAdapter<String>(mContext, cities));
        mViewCity.setCurrentItem(0);
        updateAreas();
    }

    /**
     * 根据当前的市,更新区WheelView的信息
     */
    private void updateAreas() {
        int pCurrent = mViewCity.getCurrentItem();
        mCurrentCityName = mCitisDatasMap.get(mCurrentProviceName)[pCurrent];
        String[] areas = mDistrictDatasMap.get(mCurrentCityName);

        if (areas == null || areas.length<1) {
            areas = new String[]{""};
        }
        mCurrentDistrictName = areas[0];

        mViewDistrict.setViewAdapter(new ArrayWheelAdapter<String>(mContext, areas));
        mViewDistrict.setCurrentItem(0);
    }


    /**
     * 解析省市区的XML数据
     */

//    protected void initProvinceDatas() {
//        List<ProvinceModel> provinceList = null;
//        AssetManager asset = mContext.getAssets();
//        try {
//            InputStream input = asset.open("province_data.xml");
//            // 创建一个解析xml的工厂对象
//            SAXParserFactory spf = SAXParserFactory.newInstance();
//            // 解析xml
//            SAXParser parser = spf.newSAXParser();
//            XmlParserHandler handler = new XmlParserHandler();
//            parser.parse(input, handler);
//            input.close();
//            // 获取解析出来的数据
//            provinceList = handler.getDataList();
//            // */ 初始化默认选中的省、市、区
//            if (provinceList != null && !provinceList.isEmpty()) {
//                mCurrentProviceName = provinceList.get(0).getName();
//                List<CityModel> cityList = provinceList.get(0).getCityList();
//                if (cityList != null && !cityList.isEmpty()) {
//                    mCurrentCityName = cityList.get(0).getName();
//                    List<DistrictModel> districtList = cityList.get(0)
//                            .getDistrictList();
//                    mCurrentDistrictName = districtList.get(0).getName();
//                }
//            }
//            // */
//            mProvinceDatas = new String[provinceList.size()];
//            for (int i = 0; i < provinceList.size(); i++) {
//                // 遍历所有省的数据
//                mProvinceDatas[i] = provinceList.get(i).getName();
//                List<CityModel> cityList = provinceList.get(i).getCityList();
//                String[] cityNames = new String[cityList.size()];
//                for (int j = 0; j < cityList.size(); j++) {
//                    // 遍历省下面的所有市的数据
//                    cityNames[j] = cityList.get(j).getName();
//                    List<DistrictModel> districtList = cityList.get(j)
//                            .getDistrictList();
//                    String[] distrinctNameArray = new String[districtList
//                            .size()];
//                    DistrictModel[] distrinctArray = new DistrictModel[districtList
//                            .size()];
//                    for (int k = 0; k < districtList.size(); k++) {
//                        // 遍历市下面所有区/县的数据
//                        DistrictModel districtModel = new DistrictModel(
//                                districtList.get(k).getName(), districtList
//                                .get(k).getZipcode());
//                        // 区/县对于的邮编,保存到mZipcodeDatasMap
////                        mZipcodeDatasMap.put(districtList.get(k).getName(),
////                                districtList.get(k).getZipcode());
//                        distrinctArray[k] = districtModel;
//                        distrinctNameArray[k] = districtModel.getName();
//                    }
//                    // 市-区/县的数据,保存到mDistrictDatasMap
//                    mDistrictDatasMap.put(cityNames[j], distrinctNameArray);
//                }
//                // 省-市的数据,保存到mCitisDatasMap
//                mCitisDatasMap.put(provinceList.get(i).getName(), cityNames);
//            }
//        } catch (Throwable e) {
//            e.printStackTrace();
//        } finally {
//
//        }
//    }

    /**
     * 解析省市区的XML数据
     */

    protected void getProvinceDatas() {
        List<Address> provinceList = null;
        AssetManager asset = mContext.getAssets();
        try {
            ;
            // 获取解析出来的数据
            provinceList = AddressManager.listChildByType("province", 86L);
            // */ 初始化默认选中的省、市、区
            if (provinceList != null && !provinceList.isEmpty()) {
                mCurrentProviceName = provinceList.get(0).getName();
                List<Address> cityList =AddressManager.listChild(provinceList.get(0).getId());
                if (cityList != null && !cityList.isEmpty()) {
                    mCurrentCityName = cityList.get(0).getName();
                    List<Address> districtList = AddressManager.listChild(cityList.get(0).getId());
                    mCurrentDistrictName = districtList.get(0).getName();
                }
            }
            // */
            mProvinceDatas = new String[provinceList.size()];
            for (int i = 0; i < provinceList.size(); i++) {
                // 遍历所有省的数据
                mProvinceDatas[i] = provinceList.get(i).getName();
                List<Address> cityList =AddressManager.listChild(provinceList.get(i).getId());
                String[] cityNames = new String[cityList.size()];
                for (int j = 0; j < cityList.size(); j++) {
                    // 遍历省下面的所有市的数据
                    cityNames[j] = cityList.get(j).getName();
                    List<Address> districtList =AddressManager.listChild(cityList.get(j).getId());
                    String[] distrinctNameArray = new String[districtList
                            .size()];
                    Address[] distrinctArray = new Address[districtList
                            .size()];
                    for (int k = 0; k < districtList.size(); k++) {
                        // 遍历市下面所有区/县的数据
                        Address districtModel = districtList.get(k);
                        distrinctArray[k] = districtModel;
                        distrinctNameArray[k] = districtModel.getName();
                    }
                    // 市-区/县的数据,保存到mDistrictDatasMap
                 mDistrictDatasMap.put(cityNames[j], distrinctNameArray);
                }
                // 省-市的数据,保存到mCitisDatasMap
                mCitisDatasMap.put(provinceList.get(i).getName(), cityNames);
            }
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {

        }
    }
}