• 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


    站在巨人的肩膀上,我们才干看得更远

  • 相关阅读:
    你的人生许多痛苦源于盲目较劲
    这些HTML、CSS知识点,面试和平时开发都需要 (转)
    拿什么拯救你,我的代码--c#编码规范实战篇 (转)
    最近的面试总结
    感恩和珍惜现在的生活
    我眼中的领域驱动设计(转)
    《生活就像练习》读书笔记(四)——意识状态和类型
    《生活就像练习》读书笔记(三)——发展路线
    .NET面试题解析(07)-多线程编程与线程同步 (转)
    C#进阶系列——WebApi身份认证解决方案:Basic基础认证 (转)
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/7191161.html
Copyright © 2020-2023  润新知