Now with the new json
the final code should be something like this :
You have a City
class
public class City {
private String city_id;
private String city_name;
private String province;
//Setters and Getters
Then with this City
class you can create the JSONResponse
class
public class ResponseJSON {
private String province_name;
private String province_id;
private List<City> city = null;
//Setters and Getters
This is how you call your API and add the info to the Spinner
s
apiService = ApiClient.getClient().create(ApiInterface.class);
Call<Map<String, ResponseJSON>> call = apiService.getProvinceAndCity();
call.enqueue(new Callback<Map<String, ResponseJSON>>() {
@Override
public void onResponse(Call<Map<String, ResponseJSON>>call, Response<Map<String, ResponseJSON>> response) {
responseJson = response.body();
for(Map.Entry<String, ResponseJSON> e : responseJson.entrySet())
{
provincesList.add(e.getKey());
for(City c : e.getValue().getCity()){
citiesList.add(c.getCityName());
}
}
createAdapter();
}
@Override
public void onFailure(Call<Map<String, ResponseJSON>>call, Throwable t) {
}
});
And then you have to detect the OnItemSelectedListener
event, so you can create a method to do this and update the city list according to your province
private AdapterView.OnItemSelectedListener provinceListener = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
citiesList.clear();
String provinceSelected = spinner_province.getItemAtPosition(position).toString();
for(Map.Entry<String, ResponseJSON> e : responseJson.entrySet())
{
if(e.getKey().equals(provinceSelected)){
for(City c : e.getValue().getCity()){
citiesList.add(c.getCityName());
}
}
}
cityAdapter.notifyDataSetChanged();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
Hope it helps to you, I'll put the ProvinceAndCity2SpinnerFromJson project
there so you can understand it better.