简体   繁体   English

微调器值未正确设置

[英]Spinner value not being correctly set

I am trying to set a spinner value that has been passed from another Fragment.我正在尝试设置从另一个 Fragment 传递的微调器值。 This spinner is populated with data from a LiveModel and I want just set the index of the spinner to the String value passed.这个微调器用来自 LiveModel 的数据填充,我只想将微调器的索引设置为传递的字符串值。

Currently the code below gives me the default value of the spinner as the line below returns -1;目前,下面的代码为我提供了微调器的默认值,因为下面的行返回 -1;

spinner.setSelection(adapter.getPosition(holidaySelected));

Can anyone see why this is returning -1 and not the position of the adapter at which the value sits?谁能看出为什么这会返回 -1 而不是值所在的适配器的位置?

I have read this thread: How to set selected item of Spinner by value, not by position?我已阅读此线程: 如何按值而不是按位置设置 Spinner 的选定项目?

public class PlaceInputFragment extends Fragment {

private String holidaySelected;
private HolidayViewModel holidayViewModel;
private List<String> holidayNames;
private Spinner spinner;
private ArrayAdapter<String> adapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    holidayNames = new ArrayList<>();
    spinner = v.findViewById(R.id.spinner);
    adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, holidayNames);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

    holidayViewModel = ViewModelProviders.of(this).get(HolidayViewModel.class);
    holidayViewModel.getAllHolidays().observe(this, new Observer<List<Holiday>>() {
        @Override
        public void onChanged(@Nullable final List<Holiday> holidays) {
            for (Holiday holiday : holidays) {
                holidayNames.add(holiday.getName());
            }
            adapter.notifyDataSetChanged();
        }
    });
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    if (getArguments() != null) {
    spinner = v.findViewById(R.id.spinner);

        holidaySelected = getArguments().getString("Holiday");
        spinner.setSelection(adapter.getPosition(holidaySelected));
    }
}

The issue I was having is caused by the fact that in onViewCreated when I was wanting to set the selected item in the spinner, the observer you set up to get the list of holiday names which populates the spinner with values has not yet returned.我遇到的问题是由于在 onViewCreated 中,当我想在微调器中设置所选项目时,您设置为获取用值填充微调器的假日名称列表的观察者尚未返回。 I forgot that this runs in another thread in the background.我忘了这是在后台的另一个线程中运行的。 The result is that the spinner is still empty at this point.结果是此时微调器仍然是空的。 The log statement below confirmed this下面的日志语句证实了这一点

Log.i(TAG, "Size of name list is " + holidayNames.size());

The fix that I worked with was: Create a new field for the PlaceInputFragment我使用的修复方法是:为 PlaceInputFragment 创建一个新字段

private String holidayName;

In onCreateView set its value BEFORE doing anything to initialise the spinner.在 onCreateView 中设置它的值,然后再做任何事情来初始化微调器。

if (getArguments() != null) {
            holidayName = getArguments().getString("Holiday");
} else {
            holidayName = null;
}

Finally in the observer for getAllHolidays, AFTER the line adapter.notifyDataSetChanged();最后在 getAllHolidays 的观察者中,在适配器行之后。notifyDataSetChanged(); I put the following:我把以下内容:

if (holidayName != null) {
    int i = holidayNames.indexOf(holidayName);
    spinner.setSelection(i);
}

ANOTHER way to do it would be to use safeArgs:另一种方法是使用 safeArgs:

Set holidayName in onViewCreated:在 onViewCreated 中设置假日名称:

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    if (getArguments() != null) {
PhotoInputFragmentArgs args PhotoInputFragmentArgs.fromBundle(getArguments());
holidayName = photo.getHolidayName();
}

Then, in the observer for getAllHolidays, AFTER the line adapter.notifyDataSetChanged();然后,在 getAllHolidays 的观察者中,在适配器行之后。notifyDataSetChanged(); put the following:把以下内容:

if (holidayName != null) {
    int i = holidayNames.indexOf(holidayName);
    spinner.setSelection(i);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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