2

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

 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); 
                } 
4

3 回答 3

1

我修复了它: RegionList=myDbHelper.getAllRegion(); RegionList.add(0, ""); // 为微调器创建适配器 ArrayAdapterdataAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item,RegionList); 谢谢你们!!

于 2013-05-05T13:19:33.057 回答
1

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

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

dataAdapter.addAll(RegionList);

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

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

希望这是有帮助的答案

于 2013-05-05T12:52:34.020 回答
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);
于 2013-05-05T12:32:04.937 回答