我想从我从数据库中检索的数据填充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