首页 > 代码库 > AndroidCityPicker仿IOS选择效果
AndroidCityPicker仿IOS选择效果
近期的一个项目由于android端与IOS端须要同步,所以在城市选择器这里做了一个相似IOS的CityPicker控件,当然由于本人水平问题显示效果比IOS上面还是有一定差距的。OK先让大家看下效果。
由于项目中是一个两级连选,所以这个DEMO仅仅用也就没有放上县级的数据。假设有须要实现三级连选的朋友。能够參照代码加上即可了。
内部使用的WheelView控件来自https://github.com/wangjiegulu/WheelView,在他的基础上进行和一些改动了备注,使他更适合于进行3级的城市连选,须要的朋友能够下载下来看一下。
附上部分代码片段。
数据的获取,全部的数据来自于CityManage对象。
public class CityManage {
private SQLiteDatabase database;
public CityManage(Context context){
database = CityDatabase.openDatabase(context);
}
/**
* 获取全部的省市名称
*/
public List<String> getAllNames(){
List<String> result = new ArrayList<>();
Cursor cursor = database.query("region",new String[]{"name"},null,null,null,null,null);
while (cursor.moveToNext()){
result.add(cursor.getString(0));
}
return result;
}
/**
* 获取全部省的名称
*/
public List<String> getProvinceNames(){
List<String> result = new ArrayList<>();
Cursor cursor = database.query("region",new String[]{"name"},"parent_id = 1",null,null,null,null,null);
while (cursor.moveToNext()){
result.add(cursor.getString(0));
}
return result;
}
/**
* 依据省市的ID获取省市的名称
*/
public String getProvinceNameFromProvinceId(String provinceId){
String result = "找不到桑";
Cursor cursor = database.query("region",new String[]{"name"},"region_id = ?"
,new String[]{provinceId},null,null,null,null);
while (cursor.moveToNext()){
result = cursor.getString(0);
}
return result;
}
/**
* 依据省市的名称获取省市的ID
*/
public String getProvinceIdFromProvinceName(String provinceName){
String result = "找不到桑";
Cursor cursor = database.query("region",new String[]{"region_id"},"name = ?",new String[]{provinceName},null,null,null,null);
while (cursor.moveToNext()){
result = cursor.getString(0);
}
return result;
}
/**
* 依据省的ID获取它包括的全部市的名称
*/
public List<String> getCityNameFromProvinceId(String provinceId){
List<String> result = new ArrayList<>();
Cursor cursor = database.query("region",new String[]{"name"},"parent_id = ?",new String[]{provinceId},null,null,null,null);
while (cursor.moveToNext()){
result.add(cursor.getString(0));
}
return result;
}
/**
* 依据省的ID获取它包括的全部市的模型
*/
public List<CityModel> getCityModelFromProvinceId(String provinceId){
List<CityModel> result = new ArrayList<>();
Cursor cursor = database.query("region",null,"parent_id = ?",new String[]{provinceId},null,null,null,null);
while (cursor.moveToNext()){
CityModel cityModel = new CityModel();
//城市ID
int indexId = cursor.getColumnIndex("region_id");
cityModel.setCityId(cursor.getString(indexId));
//城市名
int indexCityName = cursor.getColumnIndex("name");
cityModel.setCityName(cursor.getString(indexCityName));
//所属省ID
int indexProvinceId = cursor.getColumnIndex("parent_id");
cityModel.setProvinceId(cursor.getString(indexProvinceId));
//所属省名称
cityModel.setProvinceName(getProvinceNameFromProvinceId(cityModel.getProvinceId()));
result.add(cityModel);
}
return result;
}
/**
* 依据省市的ID获取省市的模型
*/
public CityModel getCityModelFormCityId(String cityId){
CityModel result = new CityModel();
Cursor cursor = database.query("region",null,"region_id = ?"
,new String[]{cityId},null,null,null,null);
while (cursor.moveToNext()){
//城市ID
int indexId = cursor.getColumnIndex("region_id");
result.setCityId(cursor.getString(indexId));
//城市名
int indexCityName = cursor.getColumnIndex("name");
result.setCityName(cursor.getString(indexCityName));
//所属省ID
int indexProvinceId = cursor.getColumnIndex("parent_id");
result.setProvinceId(cursor.getString(indexProvinceId));
//所属省名称
result.setProvinceName(getProvinceNameFromProvinceId(result.getProvinceId()));
}
return result;
}
/**
* 依据省市的名称获取省市的模型
*/
public CityModel getCityModelFormCityName(String cityName){
CityModel result = new CityModel();
Cursor cursor = database.query("region",null,"name = ?",new String[]{cityName},null,null,null,null);
while (cursor.moveToNext()){
//城市ID
int indexId = cursor.getColumnIndex("region_id");
result.setCityId(cursor.getString(indexId));
//城市名
int indexCityName = cursor.getColumnIndex("name");
result.setCityName(cursor.getString(indexCityName));
//所属省ID
int indexProvinceId = cursor.getColumnIndex("parent_id");
result.setProvinceId(cursor.getString(indexProvinceId));
//所属省名称
result.setProvinceName(getProvinceNameFromProvinceId(result.getProvinceId()));
}
return result;
}
}
连选关键
wvProvince.setOnWheelViewListener(new WheelView.OnWheelViewListener(){
@Override
public void onSelected(int selectedIndex, String item) {
super.onSelected(selectedIndex, item);
//又一次初始化城市数据
cityList = cityManage.getCityNameFromProvinceId(cityManage.getProvinceIdFromProvinceName(item));
//替换显示数据
wvCity.replace(cityList);
wvCity.setSeletion(0);
}
});
对这个有兴趣的朋友把源代码下载下来看一下。
源代码下载地址: http://download.csdn.net/detail/u011068996/8983575
站在巨人的肩膀上,我们才干看得更远
AndroidCityPicker仿IOS选择效果
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。