Spite从SQLite DataBase填充,空选择

时间:2013-05-05 12:29:07

标签: android android-sqlite

我正在尝试创建一个空选择的微调器,但问题是我从SQLite DataBase加载我的微调器。这是我用来加载微调器的代码:

 public void loadSpinnerDataRegion() {
       DataBaseHelper myDbHelper = new DataBaseHelper(this);
        myDbHelper = new DataBaseHelper(this);

            try {

                myDbHelper.createDataBase();

            } catch (IOException ioe) {

                throw new Error("Unable to create database");

            }

            try {

                myDbHelper.openDataBase();


            }catch(SQLException sqle){

                throw sqle;

            } 

            // Spinner Drop down elements

            RegionList=myDbHelper.getAllRegion();
             // Creating adapter for spinner
             ArrayAdapter<String>dataAdapter = new ArrayAdapter<String>(this,
                        android.R.layout.simple_spinner_item,RegionList);
              // Drop down layout style - list view with radio button
                  dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                // attaching data adapter to spinner
                spinner2.setAdapter(dataAdapter); 
                } 

3 个答案:

答案 0 :(得分:1)

试试这个

RegionList.add("");
ArrayList<String> list= myDbHelper.getAllRegion();
RegionList.addAll(list);

 ArrayAdapter<String>dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,RegionList);

答案 1 :(得分:1)

使微调器空调dataAdapter.clear();

然后在需要时再次填写;像这样:

dataAdapter.addAll(RegionList);

或者你可以对整个数组进行迭代,并单独添加每个元素,如下所示:

for (Object object: RegionList) {
            dataAdapter.add(object.getData());
        }

希望这是有用的答案

答案 2 :(得分:1)

我修好了它:     RegionList = myDbHelper.getAllRegion();         RegionList.add(0,“”);          //为微调器创建适配器          ArrayAdapterdataAdapter = new ArrayAdapter(这个,                     android.R.layout.simple_spinner_item,RegionList); 谢谢大家!!