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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:51:14+00:00 2026-05-24T23:51:14+00:00

This is my question: How do I have two spinners State and City but

  • 0

This is my question:

How do I have two spinners “State” and “City” but the city will be empty until the user selects a state first.

I am building my spinners dynamically using json data and you will see in my code below that once the state spinner value is != 0 then I use the item value of the state spinner and do another database call for my cities.

My only error is showing when I create my new ArrayAdapter to hold to the city data. I hate to post all of my code for my activity but not sure where my issue is.

public class SearchActivity extends Activity{
    private static final String TAG = "MyApp";
      @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);        

            JSONArray jsonArray;
            final JSONArray cityArray;

            try {
//GET STATE VALUES FROM DATACALL (DATABASE)
                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;

                }
    //ADD STATE VALUES TO SPINNER           
                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<?> arg0, View arg1,
                            int arg2, long arg3) {


                        int item = zipspinner.getSelectedItemPosition();

            //IF ITEM IN STATE IS SELECTED NOW GET CITIES FROM DATABALL         
                        if(item != 0){



                            try {
                                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);

                                final String[] city_spinner = new String[cityArray.length()]; 


                                for (int i=0; i<cityArray.length(); i++)
                                {                       
                                    String styleValue = cityArray.getJSONArray(i).getString(0);                 
                                    city_spinner[i] = styleValue;                               
                                }
                    //THIS IS WHERE MY ISSUE IS TRYING TO ADD THE CITIES TO THEIR SPNNER            
                                ArrayAdapter<String> adapter2 = 
                                    new ArrayAdapter<String> (this, 
                                            android.R.layout.simple_spinner_item,city_spinner);

                                adapter2.setDropDownViewResource(R.layout.city_spinner_layout);

                                cityspinner.setAdapter(adapter2);

                                cityspinner.setOnItemSelectedListener(new OnItemSelectedListener() {

                                    public void onItemSelected(AdapterView<?> arg0, View arg1,
                                            int arg2, long arg3) {
                                        int item = cityspinner.getSelectedItemPosition();


                                        if(item != 0){
                                            String item_value = array_spinner[item];
                                            String nameContentType = "name";
                                            String shopURL = "getStoreList.php?city=" + item_value;

                                            String name_data =  DataCall.getJSON(shopURL,nameContentType);

                                            Bundle bundle = new Bundle();
                                            bundle.putString("shopData", name_data);
                                            Log.v(TAG,name_data);

                                              /** Intent myIntent = new Intent(SearchActivity.this, ShowRestaurant.class);
                                               myIntent.putExtras(bundle);
                                               startActivityForResult(myIntent, 0); */
                                            }
                                        else {
                                           // finish();
                                        }



                                    }

                                    public void onNothingSelected(AdapterView<?> arg0) {
                                    }

                                });


                            }catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }



                            }
                        else {
                           // finish();
                        }



                    }

                    public void onNothingSelected(AdapterView<?> arg0) {
                    }

                });


            }catch (JSONException e) {
                // TODO Auto-generated catch block
                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-24T23:51:14+00:00Added an answer on May 24, 2026 at 11:51 pm

    Set all your Adapters and String arrays first and then just call adapter.notifyDatasetChanged() while you got the data for city. something like this:

    String city_values[] = new String[]{"Please select a state."};
    ArrayAdapter<String> adapter2 = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item, city_spinner);
    adapter2.setDropDownViewResource(R.layout.city_spinner_layout);
    cityspinner.setAdapter(adapter2);
    

    for the zipspinner implement a OnItemSelectedListener.

    zipspinner.setOnItemSelectedListener(new OnItemSelectedListener()
    {
        public void onItemSelected(AdapterView<?> parent,View view, int pos, long id) {
            String value = state_values[pos];
            // now get your city list against value.           
            city_values = yourWayOfGettingData(value);
            adapter2.notifyDatasetChanged();
        }
    
        public void onNothingSelected(AdapterView parent) {
          // Do nothing.
        }
    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables but for the purposes of this question, I will only
I'm re-asking this question but with a different framework this time. I have two
This is a quick (and probably stupid) question but if I have two consts
This might be a bit of a silly question but; If I have two
Say I have two tables (I am using Django, but this question is mostly
I have two classes [applicable to this question]. The first, XYAxes2 , extends FrameworkElement
This question is a bit opinionated, but I wanted some input. I have two
This question might sound a bit stupid but here it goes. I have two
I think I have two different questions here, I'm building off this question .
Actually, this question seems to have two parts: How to implement pattern matching? How

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.