先说一下项目当时的需求:
这个三级联动是展示在PopupWindow上的,而且是下面弹出的,选择地址后回填到相应的TextView中
在网上找了好久在博客上看到一片关于鸿洋的一篇博客
地址是:http://blog.csdn.net/lmj623565791/article/details/23382805
但是和我想要的不是一样的,但是借鉴了上面的思路,后来又找到一篇博客
地址是:http://blog.csdn.net/wulianghuan/article/details/41549189
但是数据和我的又不是一样的,所以就借鉴着上面两位大神的开始写了
首先android-wheel控件我是用的第二个地址Demo的
然后数据我是从服务器请求来的是标准的Json格式的这是实体类:仅供参考
import java.util.List; /** * 省市县实体类 * Created by Administrator on 2016/8/22. */ public class AddressBean { private List<Province> province_list; public List<Province> getProvince_list() { return province_list; } public void setProvince_list(List<Province> province_list) { this.province_list = province_list; } public class Province { private String province_code; //省份代码 private String province_name;//省份名称 private List<City> city_list; public String getProvince_code() { return province_code; } public void setProvince_code(String province_code) { this.province_code = province_code; } public String getProvince_name() { return province_name; } public void setProvince_name(String province_name) { this.province_name = province_name; } public List<City> getCity_list() { return city_list; } public void setCity_list(List<City> city_list) { this.city_list = city_list; } } public class City { private String city_code; //省份代码 private String city_name;//省份名称 private List<Area> area_list; public String getCity_code() { return city_code; } public void setCity_code(String city_code) { this.city_code = city_code; } public String getCity_name() { return city_name; } public void setCity_name(String city_name) { this.city_name = city_name; } public List<Area> getArea_list() { return area_list; } public void setArea_list(List<Area> area_list) { this.area_list = area_list; } } public static class Area { private String area_code; private String area_name; public Area( String area_name,String area_code) { this.area_code = area_code; this.area_name = area_name; } public String getArea_code() { return area_code; } public void setArea_code(String area_code) { this.area_code = area_code; } public String getArea_name() { return area_name; } public void setArea_name(String area_name) { this.area_name = area_name; } } }
之后就是开始编写具体的代码了
1.开始拼装集合
//获取省份的集合数据 List<AddressBean.Province> province_list = mAddress.getProvince_list(); mProvince = new String[province_list.size()]; for (int i=0; i< province_list.size(); i++) { // 遍历所有省的数据 mProvince[i] = province_list.get(i).getProvince_name(); //获取当前省对应的城市信息 List<AddressBean.City> cityList = province_list.get(i).getCity_list(); //创建城市数组 String[] cityNames = new String[cityList.size()]; for (int j=0; j< cityList.size(); j++) { // 遍历省下面的所有市的数据 cityNames[j] = cityList.get(j).getCity_name(); //获取当前市对应的地区信息 List<AddressBean.Area> AreaList = cityList.get(j).getArea_list(); //用于记录区/县的数组 String[] AreaArray = new String[AreaList.size()]; //区/县的邮编,这里应该记录一下,应该创建一个实体类记录一下 AddressBean.Area[] AreaCode = new AddressBean.Area[AreaList.size()]; for (int k=0; k<AreaList.size(); k++) { // 遍历市下面所有区/县的数据 AddressBean.Area area = new AddressBean.Area(AreaList.get(k).getArea_name(), AreaList.get(k).getArea_code()); // 区/县对于的邮编,保存到mAreaCode集合中 mAreaCode.put(AreaList.get(k).getArea_name(), AreaList.get(k).getArea_code()); AreaCode[k] = area; AreaArray[k] = area.getArea_name(); } // 市-区/县的数据,保存到mAreaMap集合中 mAreaMap.put(cityNames[j], AreaArray); } // 省-市的数据,保存到mCityMap集合中 mCityMap.put(province_list.get(i).getProvince_name(), cityNames);
2.设置相应的适配器
private void initAdapter() { mProvinceView.setViewAdapter(new ArrayWheelAdapter<>(context, mProvince)); mProvinceView.setVisibleItems(7); mCityView.setVisibleItems(7); mAreaView.setVisibleItems(7); updateCities(); updateAreas(); }/** * 更新城市信息 */ private void updateCities() { int pCurrent = mProvinceView.getCurrentItem(); mCurrentProvince = mProvince[pCurrent]; String[] cities = mCityMap.get(mCurrentProvince); if (cities == null) { cities = new String[]{""}; } mCityView.setViewAdapter(new ArrayWheelAdapter<>(context, cities)); mCityView.setCurrentItem(0); updateAreas(); } /** * 更新地区的信息 */ private void updateAreas() { int pCurrent = mCityView.getCurrentItem(); mCurrentCity = mCityMap.get(mCurrentProvince)[pCurrent]; String[] areas = mAreaMap.get(mCurrentCity); if (areas == null) { areas = new String[]{""}; } mAreaView.setViewAdapter(new ArrayWheelAdapter<>(context, areas)); mAreaView.setCurrentItem(0);mCurrentArea = areas[0];}
3.设置相应的监听
private void initListener(View.OnClickListener onClickListener) { // 添加change事件 mProvinceView.addChangingListener(this); // 添加change事件 mCityView.addChangingListener(this); // 添加change事件 mAreaView.addChangingListener(this); mCancel.setOnClickListener(onClickListener); mFinish.setOnClickListener(onClickListener); }
4.这个是回调的监听方法
@Override public void onChanged(WheelView wheel, int oldValue, int newValue) { // TODO Auto-generated method stub if (wheel == mProvinceView) { updateCities(); } else if (wheel == mCityView) { updateAreas(); } else if (wheel == mAreaView) { mCurrentArea = mAreaMap.get(mCurrentCity)[newValue]; } }
5.这个是相应的布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="vertical"> <RelativeLayout android:background="#fff" android:layout_width="match_parent" android:layout_height="44dp"> <TextView android:id="@+id/address_cancel" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="@dimen/distance" android:gravity="center_vertical" android:text="@string/cancel" android:textColor="#3785dc"/> <TextView android:id="@+id/address_finish" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_marginLeft="@dimen/distance" android:layout_marginRight="@dimen/distance" android:gravity="center_vertical" android:text="@string/achieve" android:textColor="#3785dc"/> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#E9E9E9" android:orientation="horizontal"> <com.example.stamp.view.widget.WheelView android:id="@+id/province" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"> </com.example.stamp.view.widget.WheelView> <com.example.stamp.view.widget.WheelView android:id="@+id/city" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"> </com.example.stamp.view.widget.WheelView> <com.example.stamp.view.widget.WheelView android:id="@+id/district" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"> </com.example.stamp.view.widget.WheelView> </LinearLayout> </LinearLayout> </RelativeLayout>这个只是总结给自己看得,也希望对其他童鞋有帮助,写的不好的地方还请见谅!