-
数据实现 Android 省市二级联动
资源介绍
spProvince = (Spinner)findViewById(R.id.spAddMettingAddressProvince);
spCity = (Spinner)findViewById(R.id.spAddMettingAddressCity);
ArrayAdapter aAdapter = ArrayAdapter.createFromResource(this, R.array.province, android.R.layout.simple_spinner_item);
aAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spProvince.setAdapter(aAdapter);
spProvince.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView> parent, View view,
int position, long id) {
Spinner spinner = (Spinner)parent;
String pro = (String)spinner.getItemAtPosition(position);
ArrayAdapter cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.city_default, android.R.layout.simple_spinner_item);
//new ArrayAdapter
// (MainActivity.this,android.R.layout.simple_spinner_item, cities);
//获取所在省含有哪些市(从资源数组文件中获取数据)
if(pro.equals("河北省")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.hebei, android.R.layout.simple_spinner_item);
}else if(pro.equals("北京市")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.bj, android.R.layout.simple_spinner_item);
}else if(pro.equals("山西省")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.shan1xi, android.R.layout.simple_spinner_item);
}else if(pro.equals("天津市")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.tianjing, android.R.layout.simple_spinner_item);
}else if(pro.equals("内蒙古")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.neimeng, android.R.layout.simple_spinner_item);
}else if(pro.equals("辽宁省")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.liaoning, android.R.layout.simple_spinner_item);
}else if(pro.equals("吉林省")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.jilin, android.R.layout.simple_spinner_item);
}else if(pro.equals("黑龙江省")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.heilongjjiang, android.R.layout.simple_spinner_item);
}else if(pro.equals("上海市")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.shanghai, android.R.layout.simple_spinner_item);
}else if(pro.equals("江苏省")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.jiangsu, android.R.layout.simple_spinner_item);
}else if(pro.equals("浙江省")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.zhejiang, android.R.layout.simple_spinner_item);
}else if(pro.equals("安徽省")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.anhui, android.R.layout.simple_spinner_item);
}else if(pro.equals("福建省")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.fujian, android.R.layout.simple_spinner_item);
}else if(pro.equals("江西省")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.jiangxi, android.R.layout.simple_spinner_item);
}else if(pro.equals("山东省")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.shandong, android.R.layout.simple_spinner_item);
}else if(pro.equals("河南省")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.henan, android.R.layout.simple_spinner_item);
}else if(pro.equals("湖北省")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.hubei, android.R.layout.simple_spinner_item);
}else if(pro.equals("湖南省")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.hunan, android.R.layout.simple_spinner_item);
}else if(pro.equals("广东省")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.guangdong, android.R.layout.simple_spinner_item);
}else if(pro.equals("广西自治区")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.guangxi, android.R.layout.simple_spinner_item);
}else if(pro.equals("海南省")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.hainan, android.R.layout.simple_spinner_item);
}else if(pro.equals("重庆市")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.chongqing, android.R.layout.simple_spinner_item);
}else if(pro.equals("四川省")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.sichuan, android.R.layout.simple_spinner_item);
}else if(pro.equals("贵州省")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.guzhou, android.R.layout.simple_spinner_item);
}else if(pro.equals("云南省")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.yunan, android.R.layout.simple_spinner_item);
}else if(pro.equals("*自治区")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.xizang, android.R.layout.simple_spinner_item);
}else if(pro.equals("陕西省")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.shan3xi, android.R.layout.simple_spinner_item);
}else if(pro.equals("甘肃省")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.gansu, android.R.layout.simple_spinner_item);
}else if(pro.equals("青海省")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.qinghai, android.R.layout.simple_spinner_item);
}else if(pro.equals("宁夏回族自治区")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.ningxia, android.R.layout.simple_spinner_item);
}else if(pro.equals("**自治区")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.*, android.R.layout.simple_spinner_item);
}else if(pro.equals("香港特别行政区")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.xianggang, android.R.layout.simple_spinner_item);
}else if(pro.equals("澳门特别行政区")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.aomen, android.R.layout.simple_spinner_item);
}else if(pro.equals("*省")){
cityAdapter = ArrayAdapter.createFromResource
(MettingAddActivity.this, R.array.*, android.R.layout.simple_spinner_item);
}
cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//绑定数据到Spinner(City)上
spCity.setAdapter(cityAdapter);
}
@Override
public void onNothingSelected(AdapterView> parent) {
}
});
- 上一篇: android省市二级联动
- 下一篇: zxing-3.3.0 (zxing 最新版本)