How to get id of selected item in dynamic spinner?

I have a counter that is populated with objects Categorythat are retrieved from db. The category table has columns _idand category_name. I want to show the category name in the counter, but when the user selects an item, I need it to get the selected item identifier. I tried the following:

Variable declaration (at the class level):

int currCategoryId;

ArrayAdapter<String> adapter;

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

Spinner spCategories;

Executing them in a method 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);

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 application crashes and I get "

The line cannot be added to the category "in this line: Category selectedCategory = (Category)spCategories.getItemAtPosition(pos);

I also tried this:

currCategoryId = view.getId();

But instead of 1 or 2 (depending on the category I selected, I currently have 2 of them), I get a very long number ...

? ?

+5
2

ArrayAdapter , ( ). , . ArrayList ArrayList ( ArrayAdapter) ,

Category selectedCategory = arrListCategories.get(pos);

onItemSelected()

+4

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 id column, 
}

get() !

+5

All Articles