Android: Spinner not showing the selected value

2019-05-14 07:15发布

问题:

I have a spinner and drop down list, the value for the spinner is getting from JSON parsing.My problem is the value is setting into the spinner but when i select a value form the drop down it is not showing in the spinner as selected, it is always blank.

I initialize the spinner as

final Spinner spinner = (Spinner) v.findViewById(R.id.spinner);
final List<String> money = new ArrayList<String>();

Assync task Api parsing onSuccess

for (int i = 0; i < jsonArray.length(); i++) {
     JSONObject c = jsonArray.getJSONObject(i);
     String amount = c.getString("amount");
     money.add(amount+" "+euro);                            
     }
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, money);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(dataAdapter);

I notice that before parsing if set a value in spinner like money.add("0"+" "+euro); , at the time all the value is showing in the spinner.

Can anyone please tell me where am wrong, why it is not showing the selected value in the spinner

回答1:

This will helps you :-

   spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long id) {
          //  selectedItem = money[position];
                     //OR
          spinner.setSelection(arrayAdapter.getPosition(pos));
         }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });