如何从Spinner中的JSON获取选定的项目ID?

时间:2019-06-15 05:23:14

标签: java android json android-spinner

如何从Spinner中的JSON获取ID?

如果我选择微调器中的项目,我想获得城市ID

示例:https://imgur.com/shvfvnO, 如果我选择“ ACEH BARAT”,则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();
            }
        });
    }

3 个答案:

答案 0 :(得分:1)

您需要创建另一个数组列表来存储ID

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

现在您需要在微调器中获取ID

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

            }
        });

答案 1 :(得分:0)

您需要在setSpinnner()方法内更新mainBinding.spinnerCity.setOnItemSelectedListener(),如下所示:

    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();
        }
    });
}

答案 2 :(得分:0)

像这样更改您的onItemSelected

 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();
        }
    });