省份城市区三级选择器

对于省份城市区三级选择器这个话题,可以说是老生常谈了。我这边的需求是,从网络上获取省份城市区的数据,然后塞到数据选择器里,效果如下。


选择省份然后城市和区会自己变动。

布局文件

<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="wrap_content"
    android:background="#E9E9E9"
    android:orientation="vertical">

    <RelativeLayout
        android:background="@color/white"
        android:padding="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tv_cancel"
            android:textColor="@color/colorPrimary"
            android:text="@string/cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/tv_sure"
            android:layout_alignParentRight="true"
            android:textColor="@color/colorPrimary"
            android:text="@string/sure"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <com.kankan.wheel.widget.WheelView
            android:id="@+id/id_province"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
        </com.kankan.wheel.widget.WheelView>

        <com.kankan.wheel.widget.WheelView
            android:id="@+id/id_city"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
        </com.kankan.wheel.widget.WheelView>

        <com.kankan.wheel.widget.WheelView
            android:id="@+id/id_district"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
        </com.kankan.wheel.widget.WheelView>
    </LinearLayout>
</LinearLayout>
每一个WheelView对应一列。

package com.wbkj.artmss.Dialog.CitySelect.service;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.hoge.app4hrzki6lz7.R;
import com.kankan.wheel.widget.OnWheelChangedListener;
import com.kankan.wheel.widget.WheelView;
import com.kankan.wheel.widget.adapters.ArrayWheelAdapter;
import com.wbkj.artmss.RequestApi.request.base.comfort.response.CityResponse;

import java.util.List;

/**
 * Created by 海峰 on 2016/11/2.
 */
public class CityTSelectDialog extends Dialog implements View.OnClickListener, OnWheelChangedListener {

    protected String[] mProvinceDatas; //省
    protected String[] cities; //市
    protected String[] mDistrictDatas; //区

    protected List<List<String>> cityList;
    protected List<List<String>> districtList;

    /**
     * 当前省的名称
     */
    protected String mCurrentProviceName;
    /**
     * 当前市的名称
     */
    protected String mCurrentCityName;

    /**
     * 当前区的名称
     */
    protected String mCurrentDistrictName;

    private Context mContext;
    private WheelView mViewProvince;
    private WheelView mViewCity;
    private WheelView mViewDistrict;
    private TextView sure,cancel;
    private CityResponse mData;
    private List<String> cs;
    private String[] ids;
    private String id;


    public CityTSelectDialog(Context context) {
        super(context,R.style.AlertDialog1);
        setContentView(R.layout.dialog_cityt_select);
        mContext = context;
        setUpViews();
        setUpListener();
    }

    public void setData(CityResponse data){
        mData = data;
        cityList = mData.cityList;
        districtList = mData.quList;
        setUpData();
    }

    public void initProvinceDatas(){
        List<String> provinceStrs = mData.pro;
        mProvinceDatas = new String[mData.pro.size()];
        for (int i=0;i<provinceStrs.size();i++){
            String str[] = provinceStrs.get(i).split(":");
            mProvinceDatas[i]=str[0];
        }
    }

    private void setUpViews() {
        mViewProvince = (WheelView) findViewById(R.id.id_province);
        mViewCity = (WheelView) findViewById(R.id.id_city);
        mViewDistrict = (WheelView) findViewById(R.id.id_district);
        sure = (TextView) findViewById(R.id.tv_sure);
        cancel =(TextView) findViewById(R.id.tv_cancel);
    }

    private void setUpListener() {
        // 添加change事件
        mViewProvince.addChangingListener(this);
        // 添加change事件
        mViewCity.addChangingListener(this);
        mViewDistrict.addChangingListener(this);
        // 添加onclick事件
        sure.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }
//设置数据
    private void setUpData() {
        initProvinceDatas();
        mViewProvince.setViewAdapter(new ArrayWheelAdapter<String>(mContext, mProvinceDatas));
        // 设置可见条目数量
        mViewProvince.setVisibleItems(14);
        mViewCity.setVisibleItems(14);
        mViewDistrict.setVisibleItems(14);
        updateCities();
        updateAreas();
    }
//滑动列表的监听
    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        // TODO Auto-generated method stub
        if (wheel == mViewProvince) {
            updateCities();
        }
        else if (wheel == mViewCity) {
            updateAreas();
        }else if (wheel == mViewDistrict) {
            mCurrentDistrictName = mDistrictDatas[mViewDistrict.getCurrentItem()];
            id = ids[mViewDistrict.getCurrentItem()];
        }
    }
//更新区的信息
    private void updateAreas() {
        int pCurrent = mViewCity.getCurrentItem();
        mCurrentCityName = cities[pCurrent];
        String[] str = cs.get(pCurrent).split(":");
        if (str.length<=2){
            mDistrictDatas = null;
        }else{
            int position = Integer.parseInt(cs.get(pCurrent).split(":")[2]);
            mDistrictDatas = new String[districtList.get(position).size()];
            ids = new String[districtList.get(position).size()];
            districtList.get(position).toArray(mDistrictDatas);
            for (int i =0;i<mDistrictDatas.length;i++){
                ids[i] = mDistrictDatas[i].split(":")[1];
                mDistrictDatas[i] = mDistrictDatas[i].split(":")[0];
            }
        }
        if (mDistrictDatas==null){
            mDistrictDatas = new String[]{""};
        }
        mViewDistrict.setViewAdapter(new ArrayWheelAdapter<String>(mContext,mDistrictDatas));
        mViewDistrict.setCurrentItem(0);
        mCurrentDistrictName = mDistrictDatas[0];
        id = ids[0];
    }

    /**
     * 根据当前的省,更新市WheelView的信息
     */
    private void updateCities() {
        int pCurrent = mViewProvince.getCurrentItem();
        cs = cityList.get(pCurrent);
        cities = new String[cs.size()];
        for (int i=0;i<cs.size();i++){
            String str[] = cs.get(i).split(":");
            cities[i]=str[0];
        }
        mCurrentProviceName = mProvinceDatas[pCurrent];
        if (cities == null) {
            cities = new String[] { "" };
        }
        mViewCity.setViewAdapter(new ArrayWheelAdapter<String>(mContext, cities));
        mViewCity.setCurrentItem(0);
        updateAreas();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_sure:
                showSelectedResult();
                dismiss();
                break;
            case R.id.tv_cancel:
                dismiss();
                break;
            default:
                break;
        }
    }
//点击监听
    private void showSelectedResult() {
        if (listener!=null){
              listener.onSelectCity(mCurrentProviceName,mCurrentCityName,mCurrentDistrictName,id);
        }
    }

    public interface OnCityClickListener{
        void onSelectCity(String str, String str1,String str2,String id);
    }

    OnCityClickListener listener;

    public void setListener(OnCityClickListener listener) {
        this.listener = listener;
    }
}
源码地址在http://download.csdn.net/detail/u013692888/9685364

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值