博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spinner 从数据库中读取数据并实现2级互动
阅读量:6402 次
发布时间:2019-06-23

本文共 3926 字,大约阅读时间需要 13 分钟。

这里主要实现了。省份和城市的二级关联。选择省份。二级对应的就是省份所有的城市
下面看代码;
        1.activity
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
                setContentView(R.layout.test_layout);
                this.mContext = this;
                Map<Integer,List> data = ProvinceDB.getProvince();//得到数据库数据
                provinceList = data.get(1);
                provinceData = data.get(0);
               
                ArrayAdapter provinceAdapter = new ArrayAdapter(mContext,android.R.layout.simple_spinner_item,provinceList);
                provinceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // 样式
               
                spProvince.setAdapter(provinceAdapter);
                spProvince.setOnItemSelectedListener(itemSelectedListener);
               
        }
       
        // 选择省份 联动 对应城市
        private OnItemSelectedListener itemSelectedListener = new OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view,
                                int position, long id) {
                        Spinner spProvince = (Spinner) parent;
                        String selProvince = (String) spProvince.getItemAtPosition(position);
                        ArrayAdapter cityAdapter = null;
                        Map<String,Integer> data = (Map)provinceData.get(position);//根据所选择的得到对应的省份id 然后得到城市
                        int pid = data.get(selProvince);//得到省份的id
                        List cityList = ProvinceDB.getCityByPid(pid);
                        cityAdapter  = new ArrayAdapter(mContext,android.R.layout.simple_spinner_item,cityList);
                        cityAdapter
                                        .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        spCity.setAdapter(cityAdapter);
                        spCity.setOnItemSelectedListener(citySelectedListener);
                }
                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                        // TODO Auto-generated method stub
                }
        };
       
        String selectedCity = null;
        private OnItemSelectedListener citySelectedListener = new OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view,
                                int position, long id) {
                        Spinner spCity = (Spinner) parent;
                        selectedCity = (String) spCity.getItemAtPosition(position);
                }
                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                        // TODO Auto-generated method stub
                }
        };
       
        2.DB类查询数据
       
        public class ProvinceDB {
        public static Map<Integer,List> getProvince(){
                String sql = "select N_PROVID ,S_PROVNAME from dict_province ";
                SQLiteDatabase db = null;
                Cursor c = null;
                 Map<Integer,List> provinceData = new HashMap<Integer,List>();
                List provinceList = null;
                try{
                        db = DatabaseHelper.getDatabase();
                        c = db.rawQuery(sql, null);
                        List provinceList1 = new ArrayList();
                        List provinceList2 = new ArrayList();
                        while(c.moveToNext()){
                                Map provinceMap = new HashMap();
                                provinceMap.put(c.getString(1), c.getInt(0));
                                provinceList1.add(provinceMap);
                                provinceList2.add(c.getString(1));
                        }
                        provinceData.put(0, provinceList1);
                        provinceData.put(1, provinceList2);
                }catch(Exception e){
                        e.printStackTrace();
                }finally{
                        if(c!=null){
                                c.close();
                        }
                        if(db!=null){
                                db.close();
                        }
                }
                return provinceData;
        }
       
        public static List<String> getCityByPid(int id){
                String sql = "select S_CITYNAME  from dict_city where N_PROVID= "+id;
                SQLiteDatabase db = null;
                Cursor c = null;
                List<String> cityList = null;
                try{
                        db = DatabaseHelper.getDatabase();
                        c = db.rawQuery(sql, null);
                        cityList = new ArrayList<String>();
                        while(c.moveToNext()){
                                cityList.add(c.getString(0));
                        }
                }catch(Exception ex){
                        ex.printStackTrace();
                }finally{
                        if(c!=null){
                                c.close();
                        }
                        if(db!=null){
                                db.close();
                        }
                }
                return cityList;
               
        }
}
3.test_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:orientation="vertical">
        <LinearLayout android:layout_width="fill_parent"
                android:layout_height="wrap_content">
                <Spinner android:id="@+id/sp_bslProvice" android:layout_width="wrap_content"
                        android:layout_height="wrap_content" android:layout_weight="1" />
                <Spinner android:id="@+id/sp_bslCity" android:layout_width="wrap_content"
                        android:layout_height="wrap_content" android:layout_weight="1" />
        </LinearLayout>

转载于:https://www.cnblogs.com/kevinGao/archive/2012/03/21/2426888.html

你可能感兴趣的文章
组策略脚本的趣味应用
查看>>
corosync+pacemaker高可用集群
查看>>
景安河南机房电力事故,部分主机和云服务器停机
查看>>
解决ubuntu系统安装完后启动项里没有ubuntu的问题
查看>>
MySQL主主
查看>>
linux的权限管理以及特殊权限SUID,SGID,Sticky
查看>>
大数据测试之初识Hadoop2
查看>>
linux安装nginx
查看>>
翻转单词顺序与左旋转字符串
查看>>
在Zf2中实现Controller按照URL自动注册
查看>>
批处理的变量引用
查看>>
oracle ORACLE_SID使用上的意义
查看>>
RHEL5下安装Xen
查看>>
2011百度之星初赛B圆环
查看>>
canvas绘制时钟
查看>>
apache配置网络驱动器
查看>>
小型企业网站的架构 & 安全配置与防护
查看>>
mysql模糊查询的优化方法--亲自实践
查看>>
Exchange Server 2013 规划系列之日志容量规划、数据库容量规划
查看>>
职场必读的经典励志故事
查看>>