首页 > 代码库 > Android城市三级联动选择器

Android城市三级联动选择器

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

/**
 * 城市Picker
 *
 * @author zihao
 *
 */
publicclass CityPicker extendsLinearLayout {
    /** 滑动控件 */fdsaf
    privateScrollerNumberPicker provincePicker;
    privateScrollerNumberPicker cityPicker;
    privateScrollerNumberPicker counyPicker;
    /** 选择监听 */
    privateOnSelectingListener onSelectingListener;
    /** 刷新界面 */
    privatestatic final int REFRESH_VIEW = 0x001;
    /** 临时日期 */
    privateint tempProvinceIndex = -1;
    privateint temCityIndex = -1;
    privateint tempCounyIndex = -1;
    privateContext context;
    privateList<cityinfo> province_list = newArrayList<cityinfo>();
    privateHashMap<string, list<cityinfo="">> city_map = newHashMap<string, list<cityinfo="">>();
    privateHashMap<string, list<cityinfo="">> couny_map = newHashMap<string, list<cityinfo="">>();
 
    privateCitycodeUtil citycodeUtil;
    privateString city_code_string;
    privateString city_string;
 
    publicCityPicker(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        getaddressinfo();
        // TODO Auto-generated constructor stub
    }
 
    publicCityPicker(Context context) {
        super(context);
        this.context = context;
        getaddressinfo();
        // TODO Auto-generated constructor stub
    }
 
    // 获取城市信息
    privatevoid getaddressinfo() {
        // TODO Auto-generated method stub
        // 读取城市信息string
        JSONParser parser = newJSONParser();
        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");
    }
 
    publicstatic class JSONParser {
        publicArrayList<string> province_list_code = newArrayList<string>();
        publicArrayList<string> city_list_code = newArrayList<string>();
 
        publicList<cityinfo> getJSONParserResult(String JSONString, String key) {
            List<cityinfo> list = newArrayList<cityinfo>();
            JsonObject result = newJsonParser().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 = newCityinfo();
 
                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());
            returnlist;
        }
 
        publicHashMap<string, list<cityinfo="">> getJSONParserResultArray(
                String JSONString, String key) {
            HashMap<string, list<cityinfo="">> hashMap = newHashMap<string, list<cityinfo="">>();
            JsonObject result = newJsonParser().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 = newArrayList<cityinfo>();
                JsonArray array = entry.getValue().getAsJsonArray();
                for(inti = 0; i < array.size(); i++) {
                    Cityinfo cityinfo = newCityinfo();
                    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);
            }
            returnhashMap;
        }
    }

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

Android城市三级联动选择器