Spinner不会改变,但适配器会改变

时间:2017-03-06 11:20:41

标签: android spinner adapter

我在我的应用中创建了Spinner,当我做某事时,我正在更改我的适配器。更改后,当我打开微调器时,它应刷新名称列表,但它保持不变。当我检查适配器时,我可以看到列表正确刷新,但它没有在微调器中显示。这是我的代码:

public void addItemsToSpinner(UserSettings settings) {
    List<Recipient> list = settings.getRecipients();

    Recipient selectedRecipient = settings.getSelectedRecipient();

    List<String> nicks = new ArrayList<>();

    if (selectedRecipient != null) {
        Log.d("utolsó","selected name " + selectedRecipient.getNickname());
        nicks.add(selectedRecipient.getNickname());
    }
    for (Recipient recipient: list) {
        if (selectedRecipient == null || !recipient.getNickname().equals(selectedRecipient.getNickname())) {

            nicks.add(recipient.getNickname());
        }
    }

    for(int i = 0 ;i<nicks.size();i++){
        Log.d("TAG","print the order = " + nicks.get(i));
    }

    dataAdapter = new ArrayAdapter<>(getActivity(), R.layout.spinner_item, nicks);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mSpinner.setAdapter(dataAdapter);
    dataAdapter.notifyDataSetChanged();
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.dialog_recipient_chooser, container, false);
    mSpinner = (Spinner) view.findViewById(R.id.spinnerChooser);
    mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            dataAdapter.notifyDataSetChanged();
            if (mListener != null) {
                mListener.onRecipientSelected(position);
            }
        }

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

        }
    });
    return view;
}

当我订购它是正确的但在旋转器中它不是。谁能告诉我为什么?

1 个答案:

答案 0 :(得分:0)

  

您是否尝试在更改列表后使用adapter.notifyDataSetChanged();