Afdal
Afdal

Reputation: 655

How to get selected item id from JSON in Spinner?

How to get id From JSON in spinner ?

i want get city id, if i choose item in spinner

example : https://i.sstatic.net/aTCJ2.jpg, if i choose "ACEH BARAT", i'll get id "512"

public void displayCities(List<CityResponse.City> cities) {
        for (CityResponse.City city : cities) {
            Log.d(TAG, city.getNama());
            spinnerItem.add(city.getNama());
            adapter.notifyDataSetChanged();
        }

    }
private void setSpinner(Context context){
        adapter = new ArrayAdapter<>(context, R.layout.spinner_item, spinnerItem);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mainBinding.spinnerCity.setAdapter(adapter);
        mainBinding.spinnerCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String citySelected = parent.getItemAtPosition(position).toString();
                Toast.makeText(context, "City : " + citySelected, Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Toast.makeText(context, "Not Selected", Toast.LENGTH_LONG).show();
            }
        });
    }

Upvotes: 0

Views: 209

Answers (3)

jayaduvanshi
jayaduvanshi

Reputation: 153

You need to create another arraylist to store id

 locationId.add(i, response.getData());

now you need to get Id in spinner

  spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                try {

                    selectedId = (String) locationId.get(position);


                } catch (Exception e) {

                }
            }

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

            }
        });

Upvotes: 1

Tejas Pandya
Tejas Pandya

Reputation: 4087

Change your onItemSelected like this

 mainBinding.spinnerCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String citySelected = parent.getItemAtPosition(position).toString();

            for (CityResponse.City city : cities) {
                if(city.getNama().equalsIgnoreCase(citySelected){
                    String city_id =  city.getCityId(); //THIS IS YOUR CITY ID
                }
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            Toast.makeText(context, "Not Selected", Toast.LENGTH_LONG).show();
        }
    });

Upvotes: 0

Hari N Jha
Hari N Jha

Reputation: 484

You need to update mainBinding.spinnerCity.setOnItemSelectedListener() inside setSpinnner() method as I have added below:

    private void setSpinner(Context context){
    adapter = new ArrayAdapter<>(context, R.layout.spinner_item, spinnerItem);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mainBinding.spinnerCity.setAdapter(adapter);
    mainBinding.spinnerCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            //Comment by Hari: You have already position available here so why you are doing this..
            //String citySelected = parent.getItemAtPosition(position).toString();

            /*Comment by Hari: If CityModel is the model of your cities ArrayList then it can be received inside spinner from the same index position as of the item as you are creating spinnerItemList in loop and both the list item position will be synced. */
            CityModel cityModel = cities.get(position); 

            String citySelected = cityModel.getCity(); //Where cities is the main array in your activity class.
            String cityId = cityModel.getCityId();

            Toast.makeText(context, "City Name: " + citySelected + "  //  City Id: cityId" + , Toast.LENGTH_LONG).show();
            //Do what you want to do next with the cityId here....

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            Toast.makeText(context, "Not Selected", Toast.LENGTH_LONG).show();
        }
    });
}

Upvotes: 0

Related Questions