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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:08:10+00:00 2026-05-25T18:08:10+00:00

How do I update my city spinner once the user selects a state? Both

  • 0

How do I update my city spinner once the user selects a state?

Both fields are populated using a DataCall.class that returns JSON data and parses the info into an array for the spinner.

My code below sets the city adapter to a defualt “Select State” value and once the user gets selects the state it should use notifyDataSetChanged since the default array for the city spinner is updated with the new city names. The errors I am getting are commented in my code below.

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

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

            final Spinner zipspinner = (Spinner) findViewById(R.id.zipspinner);
            final Spinner cityspinner = (Spinner) findViewById(R.id.cityspinner);       

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

            JSONArray jsonArray;
            try {
                String spinnerContentType = "state";
                String spinnerURL = "getStoreState.php";
                String spinner_data =  DataCall.getJSON(spinnerURL,spinnerContentType); 
                Log.d(TAG, spinner_data);
                jsonArray = new JSONArray(spinner_data);

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

                ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, 
                                  android.R.layout.simple_spinner_item,array_spinner);
                adapter.setDropDownViewResource(R.layout.state_spinner_layout);
                zipspinner.setAdapter(adapter);
                zipspinner.setOnItemSelectedListener(new OnItemSelectedListener() {
                      public void onItemSelected(AdapterView<?> parent,View view, int pos, long id) {
                            int item = zipspinner.getSelectedItemPosition();
                            if(item != 0){

                                String item_value = array_spinner[item];
                                String spinnerContentType = "city";
                                String spinnerURL = "getStoreCity.php?state=" + item_value;
                                Log.d(TAG, spinnerURL);
                                String city_data =  DataCall.getJSON(spinnerURL,spinnerContentType); 
                                Log.d(TAG, city_data);

                                JSONArray cityArray = null;
                                try {
                                    cityArray = new JSONArray(city_data);
                                } catch (JSONException e) {
                                     e.printStackTrace();
                                }       
                                final String[] city_spinner = new String[cityArray.length()]; 

                                for (int i=0; i<cityArray.length(); i++){                       
                                    String styleValue = null;
                                    try {
                                        styleValue = cityArray.getJSONArray(i).getString(0);
                                        Log.d(TAG, styleValue);
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }              
                                    city_spinner[i] = styleValue;                               
                                }

                                city_values = city_spinner; 
                                adapter2.notifyDataSetChanged();

                                String test_string = "NOTIFY UPDATE";
                                Log.d(TAG, test_string);
                            } else {
                               // finish();
                            }
                        }
                        public void onNothingSelected(AdapterView parent) {
                          // Do nothing.
                        }
                   });

                   cityspinner.setOnItemSelectedListener(new OnItemSelectedListener() {
                        public void onItemSelected(AdapterView<?> parent,View view, int pos, long id) {
                            int item = zipspinner.getSelectedItemPosition();
                            if(item != 0){

                            }else{

                            }

                        }

                        public void onNothingSelected(AdapterView parent) {
                          // Do nothing.
                        }
                  });   
          }catch (JSONException e) {
                e.printStackTrace();
          }
     }
}
  • 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-25T18:08:11+00:00Added an answer on May 25, 2026 at 6:08 pm
    public class SearchActivity extends Activity {
        ArrayAdapter<String> adapter2;
        String city_values[];
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.search_layout);
    
            final Spinner zipspinner = (Spinner) findViewById(R.id.zipspinner);
            final Spinner cityspinner = (Spinner) findViewById(R.id.cityspinner);
    
            String city_values[] = new String[]{"Please select a state."};
            adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, city_values);
            adapter2.setDropDownViewResource(R.layout.city_spinner_layout);
            cityspinner.setAdapter(adapter2);
    
            JSONArray jsonArray;
            final JSONArray cityArray;
    
            try {
                String spinnerContentType = "state";
                String spinnerURL = "getStoreState.php";
                String spinner_data = DataCall.getJSON(spinnerURL, spinnerContentType);
    
                jsonArray = new JSONArray(spinner_data);
    
                final String[] array_spinner = new String[jsonArray.length()];
    
                for (int i = 0; i < jsonArray.length(); i++) {
                    String styleValue = jsonArray.getJSONArray(i).getString(0);
                    array_spinner[i] = styleValue;
                }
    
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array_spinner);
                adapter.setDropDownViewResource(R.layout.state_spinner_layout);
                zipspinner.setAdapter(adapter);
                zipspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    
                    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                        int item = zipspinner.getSelectedItemPosition();
                        String item_value = array_spinner[item];
                        String spinnerContentType = "city";
                        String spinnerURL = "getStoreCity.php?state=" + item_value;
                        String city_data = DataCall.getJSON(spinnerURL, spinnerContentType);
                        cityArray = new JSONArray(city_data);               //The final local variable cityArray cannot be assigned, since it is defined in an enclosing type
    
                        final String[] city_spinner = new String[cityArray.length()];
    
    
                        for (int i = 0; i < cityArray.length(); i++) {
                            String styleValue = cityArray.getJSONArray(i).getString(0); //Unhandled exception type JSONException                
                            city_spinner[i] = styleValue;
                        }
    
                        city_values = city_spinner; //Unhandled exception type JSONException
                        adapter2.notifyDataSetChanged();
                    }
    
                    public void onNothingSelected(AdapterView parent {
                        // Do nothing.
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
    

    now try

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

Sidebar

Related Questions

I have a table, jos_ezrealty that has address, city, state fields and declat &
I have a User entity that has a Current Location field (city and country).
I trying to make a sql script that will randomize the city, state, and
UPDATE: rules: { firstname: asdfsadf, lastname: 123123, city: required, state: required, country: required ,
UPDATE: Focus your answers on hardware solutions please. What hardware/tools/add-in are you using to
Update: Now that it's 2016 I'd use PowerShell for this unless there's a really
i have 2 tables as fallow: users zip state city 89012 45869 0 ....
I am firing an update query that works for one page, but not another.
i am using Fileupload and 3 dropdown control in update panel, 3 dropdown will
I want to update a MapView in an iPhone app. Using a geocordinate, this

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.