How To Get Spinners Selected Item's Id That Comes From Json
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
Solution 1:
You have to create a Listener
for cities too:
spinner_city.setOnItemSelectedListener(citiesListener);
Then with this code you are able to get the cityId
privateAdapterView.OnItemSelectedListener citiesListener = newAdapterView.OnItemSelectedListener() {
@OverridepublicvoidonItemSelected(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()));
}
}
}
}
}
@OverridepublicvoidonNothingSelected(AdapterView<?> parent) {
}
};
Solution 2:
can replace activity full code
publicclassMainActivityextendsAppCompatActivity {
privateApiInterface apiService;
privateSpinner spinner_province;
privateSpinner spinner_city;
privateArrayAdapter<String> provinceAdapter;
privateArrayAdapter<String> cityAdapter;
privateArrayList<String> provincesList = newArrayList<>();
privateArrayList<String> citiesList = newArrayList<>();
privateArrayList<String> citiesIdList = newArrayList<>();
privateMap<String, ResponseJSON> responseJson;
@OverrideprotectedvoidonCreate(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(newCallback<Map<String, ResponseJSON>>() {
@OverridepublicvoidonResponse(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();
}
@OverridepublicvoidonFailure(Call<Map<String, ResponseJSON>> call, Throwable t) {
}
});
}
privatevoidinitUI() {
spinner_city = findViewById(R.id.spinner_cities);
spinner_province = findViewById(R.id.spinner_provinces);
}
privatevoidcreateAdapter() {
provinceAdapter = newArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, provincesList);
provinceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_province.setAdapter(provinceAdapter);
cityAdapter = newArrayAdapter<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);
}
privateAdapterView.OnItemSelectedListener provinceListener = newAdapterView.OnItemSelectedListener() {
@OverridepublicvoidonItemSelected(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();
}
@OverridepublicvoidonNothingSelected(AdapterView<?> parent) {
}
};
privateAdapterView.OnItemSelectedListener citiesListener = newAdapterView.OnItemSelectedListener() {
@OverridepublicvoidonItemSelected(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));
}
@OverridepublicvoidonNothingSelected(AdapterView<?> parent) {
}
};
}
Post a Comment for "How To Get Spinners Selected Item's Id That Comes From Json"