Пожалуйста, используйте этот код ниже:
private String country_id = "", city_id = "";
private ArrayList<String> country_array;
и заполнить ваш массив данными:
for (int i = 0; i < items.getResponse().size(); i++) {
String get_country_name = items.getResponse().get(i).getCode();
country_array.add(get_country_name);
}
Теперь для вашего первого адаптера блесны:
ArrayAdapter<String> country_spinnerArrayAdapter = new adapter_spinner(getActivity(), R.layout.spinner_header, country_array);
country_spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);
binding.etCountry.setAdapter(country_spinnerArrayAdapter);
binding.etCountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedItemText = parent.getSelectedItem().toString();
if (position > 0) {
country_id = selectedItemText;
city_id = "";
Load_City(position - 1); // -1 to remove postion of the static country text
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Теперь ваш город (2-й счетчик) метод:
private void Load_City(int postion) {
ArrayList<String> city_array = new ArrayList<String>();
city_array.add(getString(R.string.city));
for (int i = 0; i < items.getResponse().get(postion).getCities().size(); i++) {
String get_city_name = items.getResponse().get(postion).getCities().get(i).getName();
city_array.add(get_city_name);
}
ArrayAdapter<String> country_spinnerArrayAdapter = new adapter_spinner(getActivity(), R.layout.spinner_header, city_array);
country_spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);
binding.etCity.setAdapter(country_spinnerArrayAdapter);
binding.etCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedItemText = parent.getSelectedItem().toString();
if (position > 0) {
city_id = selectedItemText;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
надеюсь, это помогло