关闭

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

1998人阅读 评论(5) 收藏 举报
这里主要实现了。省份和城市的二级关联。选择省份。二级对应的就是省份所有的城市

下面看代码;
        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>
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:589664次
    • 积分:9227
    • 等级:
    • 排名:第2125名
    • 原创:255篇
    • 转载:170篇
    • 译文:1篇
    • 评论:111条
    最新评论