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

Android城市三级联动选择器

时间:2014-09-23 12:36:14      阅读:648      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   color   io   os   java   ar   

便捷、新颖的三级fdsafdsa联动城市选择器

 

/**
 * 城市Picker
 *
 * @author zihao
 *
 */
public class CityPicker extends LinearLayout {
    /** 滑动控件 */fdsaf
    private ScrollerNumberPicker provincePicker;
    private ScrollerNumberPicker cityPicker;
    private ScrollerNumberPicker counyPicker;
    /** 选择监听 */
    private OnSelectingListener onSelectingListener;
    /** 刷新界面 */
    private static final int REFRESH_VIEW = 0x001;
    /** 临时日期 */
    private int tempProvinceIndex = -1;
    private int temCityIndex = -1;
    private int tempCounyIndex = -1;
    private Context context;
    private List<cityinfo> province_list = new ArrayList<cityinfo>();
    private HashMap<string, list<cityinfo="">> city_map = new HashMap<string, list<cityinfo="">>();
    private HashMap<string, list<cityinfo="">> couny_map = new HashMap<string, list<cityinfo="">>();
 
    private CitycodeUtil citycodeUtil;
    private String city_code_string;
    private String city_string;
 
    public CityPicker(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        getaddressinfo();
        // TODO Auto-generated constructor stub
    }
 
    public CityPicker(Context context) {
        super(context);
        this.context = context;
        getaddressinfo();
        // TODO Auto-generated constructor stub
    }
 
    // 获取城市信息
    private void getaddressinfo() {
        // TODO Auto-generated method stub
        // 读取城市信息string
        JSONParser parser = new JSONParser();
        String area_str = FileUtil.readAssets(context, "area.json");
        province_list = parser.getJSONParserResult(area_str, "area0");
        city_map = parser.getJSONParserResultArray(area_str, "area1");
        couny_map = parser.getJSONParserResultArray(area_str, "area2");
    }
 
    public static class JSONParser {
        public ArrayList<string> province_list_code = new ArrayList<string>();
        public ArrayList<string> city_list_code = new ArrayList<string>();
 
        public List<cityinfo> getJSONParserResult(String JSONString, String key) {
            List<cityinfo> list = new ArrayList<cityinfo>();
            JsonObject result = new JsonParser().parse(JSONString)
                    .getAsJsonObject().getAsJsonObject(key);
 
            Iterator<!--?--> iterator = result.entrySet().iterator();
            while (iterator.hasNext()) {
                @SuppressWarnings("unchecked")
                Map.Entry<string, jsonelement=""> entry = (Entry<string, jsonelement="">) iterator
                        .next();
                Cityinfo cityinfo = new Cityinfo();
 
                cityinfo.setCity_name(entry.getValue().getAsString());
                cityinfo.setId(entry.getKey());
                province_list_code.add(entry.getKey());
                list.add(cityinfo);
            }
            System.out.println(province_list_code.size());
            return list;
        }
 
        public HashMap<string, list<cityinfo="">> getJSONParserResultArray(
                String JSONString, String key) {
            HashMap<string, list<cityinfo="">> hashMap = new HashMap<string, list<cityinfo="">>();
            JsonObject result = new JsonParser().parse(JSONString)
                    .getAsJsonObject().getAsJsonObject(key);
 
            Iterator<!--?--> iterator = result.entrySet().iterator();
            while (iterator.hasNext()) {
                @SuppressWarnings("unchecked")
                Map.Entry<string, jsonelement=""> entry = (Entry<string, jsonelement="">) iterator
                        .next();
                List<cityinfo> list = new ArrayList<cityinfo>();
                JsonArray array = entry.getValue().getAsJsonArray();
                for (int i = 0; i < array.size(); i++) {
                    Cityinfo cityinfo = new Cityinfo();
                    cityinfo.setCity_name(array.get(i).getAsJsonArray().get(0)
                            .getAsString());
                    cityinfo.setId(array.get(i).getAsJsonArray().get(1)
                            .getAsString());
                    city_list_code.add(array.get(i).getAsJsonArray().get(1)
                            .getAsString());
                    list.add(cityinfo);
                }
                hashMap.put(entry.getKey(), list);
            }
            return hashMap;
        }
    }

bubuko.com,布布扣

 

源码包免费下载地址::【点击这里】

Android城市三级联动选择器

标签:android   style   blog   http   color   io   os   java   ar   

原文地址:http://www.cnblogs.com/colin2014/p/3987893.html

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