Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8427039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:41:23+00:00 2026-06-10T04:41:23+00:00

I have a spinner which is populated with Category objects that are retrieved from

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:41:24+00:00Added an answer on June 10, 2026 at 4:41 am

    You can’t use the ArrayAdapter anyway because it’s for Strings only (not Categories). Hence why you’re getting a casting exception. Since you have your Category ArrayList and your String ArrayList (which is used for the ArrayAdapter) in the same order, just use

    Category selectedCategory = arrListCategories.get(pos);
    

    in your onItemSelected() method

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application, which has a Spinner that I want populated with some
Hi i have an android spinner populated from and array that i want to
So, at the moment I have a spinner which is populated from a database
I have a spinner which is populated from an Enum: public enum Frequencies {
I have a spinner which is populated from the database. This in turn is
I have a spinner witch is populated from database, when I select an item
i have an array which is populated in the spinner adapter. now i wanna
I have an android spinner that's populated by a list of strings using an
I have a simple spinner which has several number options. I want to just
I have a spinner that show my data list from a database SQLite.. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.