Android: Spinner not showing the selected value

只谈情不闲聊 提交于 2019-12-22 10:27:21

问题


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


来源:https://stackoverflow.com/questions/35216854/android-spinner-not-showing-the-selected-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!