Android动态填充微调器
我试图从数据库中检索的数据填充Android spinner。以下是检索数据的SQL语句:Android动态填充微调器,android,sql,spinner,android-arrayadapter,Android,Sql,Spinner,Android Arrayadapter,我试图从数据库中检索的数据填充Android spinner。以下是检索数据的SQL语句: private ArrayList<CategoryModel> catList = new ArrayList<CategoryModel>(); public ArrayList <CategoryModel> getAllCatName() { try { String sql = "SELECT categoryName FROM cat
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;
}
}
然而,现在的问题是,我检索到的第一个项目似乎工作正常。但是spinner中的以下项目似乎是此格式的实体。CategoryModel@61480167
。我不知道为什么会这样
提前谢谢
编辑
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。您需要一个自定义适配器,因为您的数组(cat_列表)包含自定义对象
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);
ArrayAdapter adapterFrom=新的ArrayAdapter(此,
android.R.layout.simple\u微调器\u项目,目录列表){
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图=super.getView(位置、转换视图、父级);
//在位置上获取项目
CategoryModel catModel=getItem(位置);
//使用类别名称设置textview的值
TextView TextView=(TextView)view.findViewById(android.R.id.text1);
setText(catModel.getCategoryName());
返回视图;
}
};
setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项)的适配器;
喷丝头Cat设置适配器(适配器从);
ArrayList Temp=new ArrayList();
对于(int i=0;i
adapterFrom=new ArrayAdapter(这是android.R.layout.simple\u微调器项目,
文本视图的id,临时);
adapterFrom.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉列表\u站点m) )喷丝头Cat设置适配器(适配器从)
spinnerCat.setOnItemSelectedListener
(新的MSelectedListener()
{
@凌驾
已选择公共无效项(AdapterView父项,
视图、整型位置、长id)
{
Toast.makeText(getApplicationContext(),“”+位置,Toast.LENGTH_LONG.show();
Toast.makeText(getApplicationContext(),“”+Temp.get(position),
Toast.LENGTH_LONG).show();
}
@凌驾
未选择公共无效(AdapterView父级){
//TODO自动生成的方法存根
}
});
Spinner对象在loopfor(int i=0;igetCategoryName()
时,你确定所有的cat\u list
项都返回了正确的字符串吗?请用您现在使用的代码更新您的问题。@user324977我已编辑了问题。您能帮我看一下吗?@9美元99美分不要使用任何定制的数组适配器,请改用SimpleCursorAdapter抱歉,但我如何根据我的情况实现它?抱歉,但您所说的textView id是什么意思?我没有带文本视图,谢谢。它起作用了!但是你有什么想法来确定所选项目的位置吗?好的,当然。谢谢!r u安卓开发者我的gmail id r.naveen。tamrakar@gmail.com
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;
}
}
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);
ArrayList<String> Temp = new ArrayList<String> ();
for (int i = 0; i < cat_list.size(); i++)
{
Temp.add(cat_list.get(i).getCategoryName());
}
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
}
});