I have two spinners that second spinner's value depend on the first spinner. every item in spinner has an id that came from JSON
. Now, I want to get second spinner's item id. I get selected item's name but I need name and id of the selected item.
call.enqueue(new Callback<Map<String, ResponseJSON2>>()
{
@Override
public void onResponse
(Call<Map<String, ResponseJSON2>> call, retrofit2.Response<Map<String, ResponseJSON2>> response) {
responseJson = response.body();
if (responseJson != null) {
for (Map.Entry<String, ResponseJSON2> e : responseJson.entrySet()) {
provincesList.add(e.getKey());
for (Model c : e.getValue().getModel()) {
citiesList.add(c.getName());
}
}
}
createAdapter();
}
}
Thanks.
Model.java
public class Model {
private String id;
private String name;
private String taxonomy;
private String description;
private String images;
private String parent;
//getter and setters
@Override
public String toString() {
return "model{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", taxonomy='" + taxonomy + '\'' +
", description='" + description + '\'' +
", images='" + images + '\'' +
", parent='" + parent + '\'' +
'}';
}
}
You have to create a Listener
for cities too:
spinner_city.setOnItemSelectedListener(citiesListener);
Then with this code you are able to get the cityId
private AdapterView.OnItemSelectedListener citiesListener = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String citySelected = spinner_city.getItemAtPosition(position).toString();
String provinceSelected = spinner_province.getSelectedItem().toString();
for(Map.Entry<String, ResponseJSON> e : responseJson.entrySet())
{
if(e.getKey().equals(provinceSelected)){
for(City c : e.getValue().getCity()){
if(citySelected.equals(c.getCityName())){
tv_city_id.setText(String.format("City id for %s is %s", citySelected, c.getCityId()));
}
}
}
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};