【问题标题】:How to retrieve an ID of the selected item in a dynamic Spinner?如何在动态微调器中检索所选项目的 ID?
【发布时间】:2016-10-02 21:30:56
【问题描述】:

我有一个微调器,其中填充了从数据库中检索到的 Category 对象。类别表具有 _idcategory_name 列。我想在微调器中显示类别名称,但是当用户选择一个项目时,我需要它来检索所选项目的 ID。我尝试了以下方法:

声明变量(在类级别):

int currCategoryId;

ArrayAdapter<String> adapter;

NotesManager manager = new NotesManager(this);
ArrayList<Category> arrListCategories; 
ArrayList<String> arrListCategoriesString = new ArrayList<String>();

Spinner spCategories;

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);

这是我试过的 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) {    

        }                   
    };

但在这种情况下,应用程序崩溃了,我得到一个“

无法将字符串转换为类别”在此行:Category selectedCategory = (Category)spCategories.getItemAtPosition(pos);

我也试过这个:

currCategoryId = view.getId();

但是然后不是 1 或 2(取决于我选择的类别,目前我有 2 个),我得到一个很长的数字...

我该如何解决?如何检索所选对象的 ID?

【问题讨论】:

  • 尝试 parent.getAdapter.getItem();并将此项目投射到您想要的班级,我希望这会奏效
  • @AdeelPervaiz 不,没用 - 没有 getAdapter 方法...

标签: android sqlite object spinner


【解决方案1】:

我会使用SimpleCursorAdapter,因为它存储多列,而不是只存储一列的ArrayAdapter

首先更改NotesManager.getAllCategories()以返回Cursor,它使用:

"SELECT _id, category_name FROM Table;"

如果需要,您可以按字母顺序排列结果:

"SELECT _id, category_name FROM Table ORDER BY category_name;"

下一步将此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);

终于在您的OnItemSelectedListener 中一切就绪,等待:

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    // The parameter id already refers to your Category table's id column, 
}

无需额外的get() 调用游标转换为必要的列表!

【讨论】:

    【解决方案2】:

    您无论如何都不能使用ArrayAdapter,因为它仅用于字符串(而不是类别)。因此,为什么您会遇到强制转换异常。由于您的类别ArrayList 和字符串ArrayList(用于ArrayAdapter)的顺序相同,因此只需使用

    Category selectedCategory = arrListCategories.get(pos);
    

    在您的 onItemSelected() 方法中

    【讨论】:

    • 是的,成功了!我尝试使用这条线:Category selectedCategory = (Category) parent.getItemAtPosition(pos);,但显然这不起作用。我现在看到了我的错误。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多