当前位置:操作系统 > 安卓/Android >>

Spinner 从数据库中读取数据并实现2级互动

这里主要实现了。省份和城市的二级关联。选择省份。二级对应的就是省份所有的城市

下面看代码;
        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.易做图_spinner_item,provinceList);
                provinceAdapter.setDropDownViewResource(android.R.layout.易做图_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.易做图_spinner_item,cityList);
                        cityAdapter
                                        .setDropDownViewResource(android.R.layout.易做图_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类查询数据
 &

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,