2012-08-24 47 views
5

我有一个微调器,它是从db中检索到的Category对象中填充的。分类表有_idcategory_name列。我想在微调器中显示类别名称,但是当用户选择一个项目时,我需要它来检索所选项目的ID。我试过如下:如何检索动态微调器中所选项目的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?

+0

尝试parent.getAdapter.getItem();并将此项目投到您想要的类,我希望这会工作 –

+0

@AdeelPervaiz不,没有工作 - 没有getAdapter方法... – Igal

回答

4

无论如何,您无法使用ArrayAdapter,因为它仅适用于字符串(不是类别)。因此,你为什么得到一个铸造异常。既然你有你的等级ArrayList和你的字符串ArrayList(这是用于ArrayAdapter)以相同的顺序,只需使用

Category selectedCategory = arrListCategories.get(pos); 

onItemSelected()方法

+0

是的,它的工作!我试图使用这一行:'Category selectedCategory =(Category)parent.getItemAtPosition(pos);',但显然这没有奏效。我现在看到我的错误。非常感谢你! – Igal

5

我会用一个SimpleCursorAdapter,因为它存储了多列,而不是仅存储一个的ArrayAdapter

首先变化NotesManager.getAllCategories()返回一个Cursor使用:

"SELECT _id, category_name FROM Table;" 

你可以按字母顺序排列的结果,如果你想:

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

下一页绑定这个Cursor直接到你的微调:

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()电话转换光标到必要的解释!

相关问题