简体   繁体   中英

How to select item on spinner?

i use Retrofit, item shown but i can't selected. after clicking item, toast and spinner not show anything (i can't select item on spinner).......

private List<String> spinnerItem;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        spinnerItem = new ArrayList<>();
        setSpinner();
    }

public void displayCities(List<CityResponse.City> cities) {
        mainBinding.helloText.setText(cities.get(508).getNama());
        for (CityResponse.City city : cities) {
            Log.d(TAG, city.getNama());
            spinnerItem.add(city.getNama());
        }
    }

private void setSpinner(){
        ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_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(parent.getContext(), "City : " + citySelected, Toast.LENGTH_LONG).show();
            }

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

Your setSpinner() method:

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

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

CityActivity.java:

Spinner spinnerCity = findViewById(R.id.spinner);
String[] spinnerItem = {"A", "B", "C", "D", "E"};
setSpinner(CityActivity.this, spinnerCity, Arrays.<Object>asList(spinnerItem));

Current solution:

You set the adapter in the setSpinner() with a list called spinnerItem which is empty for this moment. Then in displayCities() you fill list with values but forget to notify the adapter about new values added.

public void displayCities(List<CityResponse.City> cities) {
        for (CityResponse.City city : cities) {
            spinnerItem.add(city.getNama());
        }
        // 2. notify adapter that you have inserted new cities to the list
}

private void setSpinner(){
        // 1. set the adapter with spinnerItem which is empty here
        ArrayAdapter<String> adapter = new ArrayAdapter<>(..., ..., spinnerItem);
        mainBinding.spinnerCity.setAdapter(adapter);
        ...
}

If adapter is local variable for setSpinner() then displayCities() does not have access to the adapter . Create adapter as global and then you can call adapter.notiftDataSetChanged() in point 2 comment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM