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

  • SEARCH
  • Home
  • 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 6211001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:15:36+00:00 2026-05-24T06:15:36+00:00

So, I have two spinners, let’s call the first spinner Parent (account_type_spinner) and the

  • 0

So, I have two spinners, let’s call the first spinner Parent (account_type_spinner) and the second spinner Child (account_name_spinner). Please notice in the following code the ArrayAdapter initialization for the Child (account_name_spinner), I am feeding it a string array of account names I previously queried before the following lines of code (account_name_array):

//---define spinner objects as variables, assign adapters and listeners---

    account_type_spinner = (Spinner) findViewById(R.id.account_type_spinner);       
    account_type_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, account_type_array);
    account_type_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    account_type_spinner.setAdapter(account_type_adapter);
    account_type_spinner.setOnItemSelectedListener(new SpinnerSelectionListener());

    account_name_spinner = (Spinner) findViewById(R.id.account_name_spinner);      
    account_name_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, account_name_array);
    account_name_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    account_name_spinner.setAdapter(account_name_adapter);     

Selection in the Parent spinner triggers my “SpinnerSelectionListener” which is simply my class implementing OnItemSelectedListener. This class fires obviously each time a selection is made in the Parent spinner and the code looks like the following:

public class SpinnerSelectionListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent,
        View view, int pos, long id) {          
        String spinner_selection = parent.getItemAtPosition(pos).toString();

        if(spinner_selection.contentEquals(INCOME)) {               
            //---grab Income type accounts from db and build array---               
            db.open();
            account_name_array = db.getAccounts(INCOME);    
            account_name_adapter.notifyDataSetChanged();

            dr_amount_textview.setVisibility(View.GONE);
            dr_amount.setVisibility(View.GONE);
            cr_amount_textview.setVisibility(View.VISIBLE);
            cr_amount.setVisibility(View.VISIBLE);
            db.close();
        } else {
            //---grab Expense type accounts from db and build array---
            db.open();
            account_name_array = db.getAccounts(EXPENSE);
            account_name_adapter.notifyDataSetChanged();

            cr_amount_textview.setVisibility(View.GONE);
            cr_amount.setVisibility(View.GONE);
            dr_amount_textview.setVisibility(View.VISIBLE);
            dr_amount.setVisibility(View.VISIBLE);
            db.close();
        }
    }

    public void onNothingSelected(AdapterView<?> parent) {
      // Do nothing.
    }
}

The code above should update the Child (account_name_spinner) with INCOME or EXPENSE accounts whenever the Parent’s selection changes from INCOME to EXPENSE or vice versa (this is an accounting app). The updating of the Child’s spinner list should be facilitated by the “account_name_adapter.notifyDataSetChanged();” however nothing is happening.

I looked into this issue further on StackOverflow and found that I must .clear() or .remove() items from my Child’s ArrayAdapter (account_name_adapter) before the list will update, however, when I try “account_name_adapter.clear();” before notifying the ArrayAdapter I get an error that the operation is illegal. Any ideas what I am doing wrong?

  • 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-05-24T06:15:36+00:00Added an answer on May 24, 2026 at 6:15 am

    You can just reassign the adapter after you got the updated array (account_name_array)

    account_name_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, account_name_array);
    account_name_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    account_name_spinner.setAdapter(account_name_adapter);
    

    EDIT: To the adapter you don’t pass a reference, so when you update the array the adapter still have the same data. Calling .notifyDataSetChanged() do not update the adapter with the new array.

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

Sidebar

Related Questions

I have two spinners in an activity, A and B. Each spinner uses an
i have two spinners. If in the first one the Item Diesel is selected
I have two spinners one named cuisine and other area. I m calling the
I have two spinners (day_spin and time_spin) in one Activity . I want to
I have a custom spinner that uses two custom views, one for the drop
I have 2 spinner, each spinner's data loaded from database using AsyncTask i call
I got two people with Motorola phones comlaining that my spinners have white text
I have two spinners that trigger the onItemSelected event. The question is How can
Edit: Fixed it by using the same adapter on both spinners. I have two
I have two spinner in my system. Now I have to change the selected

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.