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 6587383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:54:37+00:00 2026-05-25T16:54:37+00:00

QUESTION: If I set a variable prior to the onCreate can I use notifyDataSetChanged()

  • 0

QUESTION: If I set a variable prior to the onCreate can I use notifyDataSetChanged() to update an adapter that uses that array later?

I am instigating my city_values array prior to my onCreate -This is the only way I can get the script not to show any errors. But once the user selects a state from its spinner it should use notifyDataSetChanged() to update the adapter that attaches the city_values array. Below is a small section of my code. I think my issue has to do with the city_value being set to early. How can I get around this?

public class SearchActivity extends Activity{
    ArrayAdapter<String> adapter2;
    String city_values[] = new String[]{"Please select a state first."};

      @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.search_layout)

           adapter2 = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item, city_values);
           adapter2.setDropDownViewResource(R.layout.city_spinner_layout);
           cityspinner.setAdapter(adapter2);


//On select of State spinner use item value to query and get citys reassign those values back to city_values and then tell adapter2 notifyDataSetChanged()


           for (int i=0; i<jsonArray.length(); i++)
            {   
                String styleValue = jsonArray.getJSONArray(i).getString(0);    
                Log.d(TAG, styleValue);
                city_spinner[i] = styleValue;
            }
               city_values = city_spinner; 

               adapter2.notifyDataSetChanged();
  • 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-25T16:54:38+00:00Added an answer on May 25, 2026 at 4:54 pm

    It would be better to declare an ArrayList and then add the content in ArrayList and set the data to the adapter and notify.

     public class SearchActivity extends Activity{
        ArrayAdapter<String> adapter2;
        ArrayList<String> city_values = new ArrayList<String>();
    
          @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.search_layout)
    
                city_values.add("your content");
    
               adapter2 = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item, city_values);
               adapter2.setDropDownViewResource(R.layout.city_spinner_layout);
               cityspinner.setAdapter(adapter2);
    

    Now if you want to update another spinner on this cityspinner selected item, you can take another ArrayList in the same way and add the items in that and set the Adapter.

    UPDATE
    

    Take an ArrayList<String> city_spinner_array = new ArrayList<String>;

    for (int i=0; i<jsonArray.length(); i++)
                {   
                    String styleValue = jsonArray.getJSONArray(i).getString(0);    
                    Log.d(TAG, styleValue);
                    city_spinner_array.add(styleValue);
                }
    

    And, now you will have your new values in city_spinner_array. So, set the adapter and you did for the previous spinner.

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

Sidebar

Related Questions

How could I set a variable that I can read by using eval('productOptionTree' +
Old question My understanding is that C# has in some sense HashSet and set
SHORT VERSION OF QUESTION: So basically my question is: How can I set the
Dumb question.. what logic does the database use to determine the result set if
This question is for experiences programmers. Do you have a set process that you
I want to change the effects of a set variable that is at the
I want to use a spinner to set a variable to an url. Every
Question: What is the best way to set focus to a web control in
I have a question about how to set up the relations between two models
I have to set GridView.DataKeyNames (This is really a question about the syntax for

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.