Iga*_*gal 5 sqlite android object spinner
我有一个微调器,其中填充了Category从db中检索的对象.Categories表包含列_id和category_name列.我想在微调器中显示类别名称,但是当用户选择一个项目时,我需要它来检索所选项目的ID.我尝试了以下方法:
声明变量(在类级别):
int currCategoryId;
ArrayAdapter<String> adapter;
NotesManager manager = new NotesManager(this);
ArrayList<Category> arrListCategories;
ArrayList<String> arrListCategoriesString = new ArrayList<String>();
Spinner spCategories;
Run Code Online (Sandbox Code Playgroud)
在onCreate方法中实例化它们:
manager.getAllCategories();
arrListCategories = manager.getAllCategories();
for (int i = 0; i < arrListCategories.size(); i++)
{
Category currCategory = arrListCategories.get(i);
arrListCategoriesString.add(currCategory.getCategory_name().toString());
}
adapter=new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, arrListCategoriesString);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spCategories.setAdapter(adapter);
spCategories.setOnItemSelectedListener(spinnerListener);
Run Code Online (Sandbox Code Playgroud)
这是我试过的spinnerListener:
OnItemSelectedListener spinnerListener = new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected.
//currCategory = (String) parent.getItemAtPosition(pos).toString();
//selectedCategory =
Category selectedCategory = (Category)spCategories.getItemAtPosition(pos);
currCategoryId = selectedCategory.getId();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
};
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,应用程序崩溃,我得到一个"
字符串无法强制转换为此行的类别:
Category selectedCategory = (Category)spCategories.getItemAtPosition(pos);
我也试过这个:
currCategoryId = view.getId();
Run Code Online (Sandbox Code Playgroud)
但后来不是1或2(取决于我选择的类别,目前我有2个),我的数字很长......
我该如何解决?如何检索所选对象的ID?
我会使用a,SimpleCursorAdapter因为它存储多个列,而不是ArrayAdapter只存储一个列.
第一次更改NotesManager.getAllCategories()返回Cursor使用:
"SELECT _id, category_name FROM Table;"
Run Code Online (Sandbox Code Playgroud)
如果需要,您可以按字母顺序排列结果:
"SELECT _id, category_name FROM Table ORDER BY category_name;"
Run Code Online (Sandbox Code Playgroud)
接下来将这个Cursor直接绑定到您的Spinner:
Cursor cursor = manager.getAllCategories();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cursor, new String[] {"category_name"}, new int[] {android.R.id.text1});
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spCategories.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)
最后在你的OnItemSelectedListener一切准备就绪并等待:
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// The parameter id already refers to your Category table's id column,
}
Run Code Online (Sandbox Code Playgroud)
无需额外get()调用或将游标转换为列表!
小智 4
无论如何你都不能使用它,ArrayAdapter因为它仅适用于字符串(不适用于类别)。这就是为什么你会遇到强制转换异常。由于您的类别ArrayList和字符串ArrayList(用于ArrayAdapter)的顺序相同,因此只需使用
Category selectedCategory = arrListCategories.get(pos);
Run Code Online (Sandbox Code Playgroud)
在你的onItemSelected()方法中
| 归档时间: |
|
| 查看次数: |
3816 次 |
| 最近记录: |