2014-08-29 109 views
0

我想从我从数据库中检索的数据填充Android微调框。这是我的SQL语句检索数据:Android动态填充微调框

private ArrayList<CategoryModel> catList = new ArrayList<CategoryModel>(); 
public ArrayList <CategoryModel> getAllCatName() { 
    try { 
     String sql = "SELECT categoryName FROM category"; 
     Cursor mCur = mDb.rawQuery(sql, null); 
     Log.e(TAG, "Data Grab Success"); 
     if (mCur.getCount() != 0) { 
      if (mCur.moveToFirst()) { 
       do { 
        CategoryModel cm = new CategoryModel(); 
        cm.setCategoryName((mCur.getString(mCur 
          .getColumnIndex("categoryName")))); 

        catList.add(cm); 
       } while (mCur.moveToNext()); 
      } 
     } 
     return catList; 
    } catch (SQLException mSQLException) { 
     throw mSQLException; 
    } 
} 

这里是我尝试将数据绑定到微调代码:

spinnerCat = (Spinner) dialogView.findViewById(R.id.spinnerCat); 

    ArrayList<CategoryModel> cat_list = cc.getAllCatName(); 
    ArrayAdapter<CategoryModel> adapterFrom = new ArrayAdapter<CategoryModel>(this, 
      android.R.layout.simple_spinner_item, cat_list) { 
     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View view = super.getView(position, convertView, parent); 

      // Get item at position 
      CategoryModel catModel = getItem(position); 
      // Set textview's value with the category name 
      TextView textView = (TextView) view.findViewById(android.R.id.text1); 
      textView.setText(catModel.getCategoryName()); 

      return view; 
     } 
    }; 

    adapterFrom.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spinnerCat.setAdapter(adapterFrom); 

在我的实体类:

public String categoryName; 
public String getCategoryName() { 
    return categoryName; 
} 

public void setCategoryName(String categoryName) { 
    this.categoryName = categoryName; 
} 

但是,现在的问题是我检索到的第一个项目似乎工作正常。但是spinner中的下列项目似乎采用这种格式[email protected]。我想知道为什么会这样。

在此先感谢。

编辑

DatabaseAdapter mDbHelper = new DatabaseAdapter(this); 
    mDbHelper.createDatabase(); 
    mDbHelper.open(); 
    CategoryController cc = new CategoryController(
      mDbHelper.open()); 

    Cursor cursor = cc.getAllCatName(); 
    if(cursor.getCount()>0){ 
      String[] from = new String[]{"categoryName"}; 
      // create an array of the display item we want to bind our data to 
      int[] to = new int[]{android.R.id.text1}; 
      SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, 
      cursor, from, to); 
      mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
      spinnerCat.setAdapter(mAdapter); 
      } 

我的SQL语句的方法:

public Cursor getAllCatName() { 
    try { 
     String sql = "SELECT categoryName FROM category"; 
     Cursor mCur = mDb.rawQuery(sql, null); 
     Log.e(TAG, "Data Grab Success"); 
     if (mCur.getCount() != 0) { 
      if (mCur.moveToFirst()) { 
       do { 
        CategoryModel cm = new CategoryModel(); 
        cm.setCategoryName((mCur.getString(mCur 
          .getColumnIndex("categoryName")))); 

        catList.add(cm); 
       } while (mCur.moveToNext()); 
      } 
     } 
     return mCur; 
    } catch (SQLException mSQLException) { 
     throw mSQLException; 
    } 
} 

而且新的错误消息是列_id不存在。我真的不知道为什么会这样,因为我从来没有在任何地方宣布过任何_id。

+0

微调对象创建mulipal时间在循环 – 2014-08-29 07:39:34

+0

对(INT I = 0; I (此, android.R.layout.simple_spinner_item, cat_list.get(ⅰ).getCategoryName()); adapterFrom.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinnerCat.setAdapter(adapterFrom); }这一行中的问题 – 2014-08-29 07:40:07

+0

是的,但我不知道如何将一个对象添加到微调框中。你有什么主意吗? – 2014-08-29 07:41:01

回答

1
ArrayList<String> Temp = new ArrayList<String>(); 
for (int i = 0; i < cat_list.size(); i++) 
{ 
    Temp.add(cat_list.get(i).getCategoryName()); 
} 

adapterFrom =新ArrayAdapter(此,android.R.layout.simple_spinner_item, ID的TextView的,温度); adapterFrom.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_ite m);
spinnerCat.setAdapter(adapterFrom);

spinnerCat.setOnItemSelectedListener 
    (new OnItemSelectedListener() 
     { 
     @Override 
     public void onItemSelected(AdapterView<?> parent, 
       View view, int position, long id) 
     { 
      Toast.makeText(getApplicationContext(), ""+position, Toast.LENGTH_LONG).show(); 
      Toast.makeText(getApplicationContext(), ""+Temp.get(position), 
      Toast.LENGTH_LONG).show(); 

     } 

     @Override 
     public void onNothingSelected(AdapterView<?> parent) { 
      // TODO Auto-generated method stub 

     } 
    }); 
+0

对不起,但你是什么意思的id的textView?我没有一个textview与我tho。 – 2014-08-29 08:24:32

+0

谢谢。有用!但是,你有任何想法来获得选定项目的位置? – 2014-08-29 08:28:20

+0

更新的代码请检查 – 2014-08-29 08:52:38

1

您需要一个自定义适配器,因为您的数组(cat_list)包含自定义对象。

ArrayAdapter<CategoryModel> adapterFrom = new ArrayAdapter<CategoryModel>(this, 
     android.R.layout.simple_spinner_item, cat_list) { 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = super.getView(position, convertView, parent); 

     // Get item at position 
     CategoryModel catModel = getItem(position); 
     // Set textview's value with the category name 
     TextView textView = (TextView) view.findViewById(android.R.id.text1); 

     textView.setText(catModel.getCategoryName()); 

     return view; 
    } 
}; 

adapterFrom.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
spinnerCat.setAdapter(adapterFrom); 
+0

但是,使用这些代码,它只会返回一个正确的记录。其余记录如[email protected]等。你有什么主意吗? – 2014-08-29 07:50:39

+0

当你调用'getCategoryName()'时,你确定所有'cat_list'的项都返回一个正确的String吗?请使用您现在使用的代码更新您的问题。 – Simas 2014-08-29 07:52:46

+0

@ user324977我编辑了这个问题。你能帮我看一下吗? – 2014-08-29 07:56:42