我想從我從數據庫中檢索的數據填充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。
微調對象創建mulipal時間在循環 – 2014-08-29 07:39:34
對(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
是的,但我不知道如何將一個對象添加到微調框中。你有什麼主意嗎? – 2014-08-29 07:41:01