Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve an ID of the selected item in a dynamic Spinner?

I have a spinner which is populated with Category objects that are retrieved from the db. The Categories table has _id and category_name columns. I want to show the category name in the spinner, but when the user selects an item, I need it to retrieve the selected item's ID. I tried the following:

Declaring variables (in class level):

int currCategoryId;

ArrayAdapter<String> adapter;

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

Spinner spCategories;

Instantiating them in onCreate method:

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

And this is the spinnerListener I tried:

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

        }                   
    };

But in this case the app crashes and I'm getting a "

String cannot be cast to Category" at this line: Category selectedCategory = (Category)spCategories.getItemAtPosition(pos);

I also tried this:

currCategoryId = view.getId();

But then instead of 1 or 2 (depending on what category I selected, currently I have 2 of them), I'm getting a very long number...

How can I fix it? How can I retrieve the ID of the selected object?

like image 600
Igal Avatar asked Aug 24 '12 16:08

Igal


1 Answers

I would use a SimpleCursorAdapter because it stores multiple columns, instead of an ArrayAdapter that only stores one.

First change NotesManager.getAllCategories() to return a Cursor that uses:

"SELECT _id, category_name FROM Table;"

You could alphabetize the results if you want:

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

Next bind this Cursor straight to your 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);

Finally in your OnItemSelectedListener everything is ready and waiting:

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

No extra get() calls or converting Cursors into Lists necessary!

like image 148
Sam Avatar answered Sep 20 '22 20:09

Sam