码迷,mamicode.com
首页 > 移动开发 > 详细

AndroidCityPicker仿IOS选择效果

时间:2015-08-10 12:09:19      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:android   控件   citypicker   城市选择器   

最近的一个项目因为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选择效果

标签:android   控件   citypicker   城市选择器   

原文地址:http://blog.csdn.net/u011068996/article/details/47395065

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!