может заменить полный код активности
public class MainActivity extends AppCompatActivity {
private ApiInterface apiService;
private Spinner spinner_province;
private Spinner spinner_city;
private ArrayAdapter<String> provinceAdapter;
private ArrayAdapter<String> cityAdapter;
private ArrayList<String> provincesList = new ArrayList<>();
private ArrayList<String> citiesList = new ArrayList<>();
private ArrayList<String> citiesIdList = new ArrayList<>();
private Map<String, ResponseJSON> responseJson;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
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) {
}
});
}
private void initUI() {
spinner_city = findViewById(R.id.spinner_cities);
spinner_province = findViewById(R.id.spinner_provinces);
}
private void createAdapter() {
provinceAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, provincesList);
provinceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_province.setAdapter(provinceAdapter);
cityAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, citiesList);
cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_city.setAdapter(cityAdapter);
spinner_province.setOnItemSelectedListener(provinceListener);
spinner_city.setOnItemSelectedListener(citiesListener);
}
private AdapterView.OnItemSelectedListener provinceListener = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
citiesList.clear();
citiesIdList.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());
citiesIdList.add(c.getCityId());
}
}
}
cityAdapter.notifyDataSetChanged();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
private AdapterView.OnItemSelectedListener citiesListener = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int city_id = Integer.parseInt(citiesIdList.get(position));
Log.i("your_city_id", String.valueOf(city_id));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
}