Android实现城市选择三级联动攻略
在Android开发中,有时会需要实现城市选择功能,而且还需要支持三级联动,即选择省份后展示该省份下的市区列表,选择城市后展示该城市下的县区列表。这里提供一份Android实现城市选择三级联动的攻略。
一、搭建数据源
实现城市选择三级联动首先需要有一个完整的城市数据源,可以采用第三方库提供的数据,也可以自己搭建。以下是自己搭建数据源的示例代码:
// Province.java省份实体类
public class Province {
public String name; // 省名称
public List<City> cities; // 省下的城市列表
public Province(String name, List<City> cities) {
this.name = name;
this.cities = cities;
}
}
// City.java城市实体类
public class City {
public String name; // 城市名称
public List<County> counties; // 城市下的县区列表
public City(String name, List<County> counties) {
this.name = name;
this.counties = counties;
}
}
// County.java县区实体类
public class County {
public String name; // 县区名称
public County(String name) {
this.name = name;
}
}
// 初始化数据源
List<Province> provinceList = new ArrayList<>();
List<City> cities1 = new ArrayList<>();
cities1.add(new City("杭州", Arrays.asList(new County("滨江"), new County("拱墅"))));
cities1.add(new City("宁波", Arrays.asList(new County("海曙"), new County("江北"))));
provinceList.add(new Province("浙江", cities1));
List<City> cities2 = new ArrayList<>();
cities2.add(new City("北京", Arrays.asList(new County("东城"), new County("西城"), new County("朝阳"))));
cities2.add(new City("天津", Arrays.asList(new County("和平"), new County("河北"))));
provinceList.add(new Province("北京", cities2));
二、实现三级联动选择功能
第二步需要实现以下功能:
-
展示省份列表
-
选择省份后展示该省份下的城市列表
-
选择城市后展示该城市下的县区列表
以下是示例代码:
// CityPickerActivity.java城市选择界面
public class CityPickerActivity extends AppCompatActivity {
private List<Province> provinceList; // 省份数据源
private String[] pArray, cArray, coArray; // 省、市、县区列表
private int pIndex, cIndex, coIndex; // 省、市、县区选中索引
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_city_picker);
// 初始化数据源
provinceList = initData();
initViews();
}
// 初始化数据源
private List<Province> initData() {
// 省、市、县区数据源初始化代码(同上述代码)
}
// 初始化视图
private void initViews() {
// 省份选择器
final NumberPicker provincePicker = findViewById(R.id.province_picker);
pArray = new String[provinceList.size()];
for (int i = 0; i < provinceList.size(); i++) {
pArray[i] = provinceList.get(i).name;
}
provincePicker.setMinValue(0);
provincePicker.setMaxValue(pArray.length - 1);
provincePicker.setDisplayedValues(pArray);
provincePicker.setWrapSelectorWheel(false);
provincePicker.setOnValueChangedListener((picker, oldVal, newVal) -> {
pIndex = newVal;
// 切换省份后更新市的数据源和界面
cIndex = 0;
coIndex = 0;
CityPickerAdapter adapter = (CityPickerAdapter) cityPicker.getAdapter();
adapter.update(provinceList.get(pIndex).cities);
countyPicker.setAdapter(new CountyPickerAdapter(provinceList.get(pIndex).cities.get(0).counties));
});
// 城市选择器
final NumberPicker cityPicker = findViewById(R.id.city_picker);
String[] cArray = new String[provinceList.get(0).cities.size()];
for (int i = 0; i < provinceList.get(0).cities.size(); i++) {
cArray[i] = provinceList.get(0).cities.get(i).name;
}
cityPicker.setMinValue(0);
cityPicker.setMaxValue(cArray.length - 1);
cityPicker.setDisplayedValues(cArray);
cityPicker.setWrapSelectorWheel(false);
cityPicker.setOnValueChangedListener((picker, oldVal, newVal) -> {
cIndex = newVal;
// 切换城市后更新县区的数据源和界面
coIndex = 0;
CountyPickerAdapter adapter = (CountyPickerAdapter) countyPicker.getAdapter();
adapter.update(provinceList.get(pIndex).cities.get(cIndex).counties);
});
// 县区选择器
final NumberPicker countyPicker = findViewById(R.id.county_picker);
String[] coArray = new String[provinceList.get(0).cities.get(0).counties.size()];
for (int i = 0; i < provinceList.get(0).cities.get(0).counties.size(); i++) {
coArray[i] = provinceList.get(0).cities.get(0).counties.get(i).name;
}
countyPicker.setMinValue(0);
countyPicker.setMaxValue(coArray.length - 1);
countyPicker.setDisplayedValues(coArray);
countyPicker.setWrapSelectorWheel(false);
countyPicker.setOnValueChangedListener((picker, oldVal, newVal) -> {
coIndex = newVal;
});
}
以上代码是以NumberPicker为例,其他控件选择方式类似。
三、获取选择结果
最后一步需要获取选择的结果,可能需要使用SharedPreferences、回调函数等方法返回结果,这里只提供SharedPreferences存储选择结果的示例代码:
// 选择结果存储方法
private void storeResult() {
SharedPreferences sp = getSharedPreferences("city", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("province", pArray[pIndex]);
editor.putString("city", cArray[cIndex]);
editor.putString("county", coArray[coIndex]);
editor.apply();
}
// 获取选择结果方法
private String getResult() {
SharedPreferences sp = getSharedPreferences("city", MODE_PRIVATE);
String province = sp.getString("province", "");
String city = sp.getString("city", "");
String county = sp.getString("county", "");
return province + " - " + city + " - " + county;
}
以上就是Android实现城市选择三级联动的攻略,通过自己搭建数据源并使用NumberPicker作为选择控件,最后可以通过SharedPreferences存储选择结果。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Android实现城市选择三级联动 - Python技术站