Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

android - 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 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 + ''' +
                '}';
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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) {

        }
    };

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...