2014-08-29 75 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