Based On Selection Display Items In Second Spinner
i am trying to get states in second spinner based on selection in first spinner.if i select india getting all the states but i don't want like that,i need only india states,please
Solution 1:
You can try this
publicclassCountry {
privateString name;
privateString code;
privateList<State> states;
publicCountry(String name, String code, List<State> states) {
this.name = name;
this.code = code;
this.states = states;
}
publicStringgetName() {
return name;
}
publicStringgetCode() {
return code;
}
publicList<State> getStates() {
return states;
}
@OverridepublicStringtoString() {
return name;
}
}
publicclassState {
privateString name;
privateList<City> cities;
publicState(String name, List<City> cities) {
this.name = name;
this.cities = cities;
}
publicStringgetName() {
return name;
}
publicList<City> getCities() {
return cities;
}
@OverridepublicStringtoString() {
return name;
}
}
publicclassCity {
privateString name;
privateString code;
publicCity(String name, String code) {
this.name = name;
this.code = code;
}
publicStringgetName() {
return name;
}
publicStringgetCode() {
return code;
}
@OverridepublicStringtoString() {
return name;
}
}
Then build your data from JSON like this
try {
JSONObject jsonObject = newJSONObject(loadJsonFromAsset());
JSONArray jsonArray = jsonObject.getJSONArray("countries");
List<Country> countries;
List<City> cities;
List<State> states;
countries = newArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject countries_object = jsonArray.getJSONObject(i);
// get country name and codeString country_name = countries_object.getString("name");
String country_code = countries_object.getString("countryCode");
// get country statesJSONArray states_array = countries_object.getJSONArray("states");
states = newArrayList<>();
for (int j = 0; j < states_array.length(); j++) {
JSONObject states_object = states_array.getJSONObject(j);
// get state nameString state_name = states_object.getString("name");
// get all city for this stateJSONArray city_array = states_object.getJSONArray("cities");
cities = newArrayList<>();
for (int k = 0; k < city_array.length(); k++) {
JSONObject city_object = city_array.getJSONObject(k);
// get city name and codeString city_name = city_object.getString("name");
String city_code = city_object.getString("code");
// add new city
cities.add(newCity(city_name, city_code));
}
// add new state with cities
states.add(newState(state_name, cities));
}
countries.add(newCountry(country_name, country_code, states));
}
ArrayAdapter<Country> country_adapter = newArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, countries);
country_adapter.setAdapter(mCountryAdapter);
country_adapter.setOnItemSelectedListener(this);
} catch (JSONException e) {
e.printStackTrace();
}
And finally
@OverridepublicvoidonItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Countrycountry= (Country) mCountrySpinner.getSelectedItem();
mStateSpinner.setAdapter(newArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, country.getStates()));
}
Solution 2:
String countryCode = null;
You can call your second spinner in onItemSelected method.
countryCode = countrySpinner.getSelectedItem().toString();
// call your second adapter
before populate state list
HashMap<String, String> hmap = newHashMap<String, String>();
hmap.put(country_code, stateName);
Post a Comment for "Based On Selection Display Items In Second Spinner"