alireza
alireza

Reputation: 89

populate province and city in two spinner using json

I have two spinner that the first one contains province name and the second one spinner depends on first spinner value that contains city name. I'm using json for this app and readed some article and toturial but I can't do it right this is my json link : Link

any guide will be helpfull

thanks in advance

Upvotes: 1

Views: 150

Answers (1)

Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20646

Now with the new json the final code should be something like this :

You have a City class

public class City {
    private String city_id;
    private String city_name;
    private String province;
    //Setters and Getters

Then with this City class you can create the JSONResponse class

public class ResponseJSON {
    private String province_name;
    private String province_id;
    private List<City> city = null;
    //Setters and Getters

This is how you call your API and add the info to the Spinners

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

            }
        });

And then you have to detect the OnItemSelectedListener event, so you can create a method to do this and update the city list according to your province

private AdapterView.OnItemSelectedListener provinceListener = new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            citiesList.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());
                    }
                }
            }
            cityAdapter.notifyDataSetChanged();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    };

Hope it helps to you, I'll put the ProvinceAndCity2SpinnerFromJson project there so you can understand it better.

Upvotes: 2

Related Questions