首页 > 代码库 > Android城市三级联动选择器
Android城市三级联动选择器
便捷、新颖的三级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;
}
}
源码包免费下载地址::【点击这里】
Android城市三级联动选择器
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。