Populate the city meter after the state meter is selected

This is my question:

Like I have two spinners "State" and "City", but the city will be empty until the user first selects the state.

I am building a speaker using json data and you will see in my code below that after the spinner value of the state is equal to! = 0, I use the item value for the status counter and make another database call for my cities.

My only mistake is showing when I create my new ArrayAdapter to store city data. I don’t like publishing all my code for my activity, but I don’t know where my problem 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(); } } } 
+3
source share
1 answer

First, install all of your Adapter and String arrays, and then just call adapter.notifyDatasetChanged() until you get the data for the city. something like that:

 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 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. } }); 
+1
source

All Articles