微调器没有 select 项

Spinner does not select item

我已经用这个很长时间了,但我无法让它工作...我有一个微调器,由我的 class 中的日期列表填充,它扩展了 Fragment .但是,当我 select 来自下拉微调器的日期之一时,什么也没有发生。它应该更改文本视图的文本并在微调器中显示 selected 项目...

我一直在阅读 Spinners 的其他示例,有些人说它不可点击,但后来我无法打开 Spinner...

我也看到示例说在创建适配器时传入 "this" 但我开始收到一个错误 "cannot resolve constructor..."

别人说用"spinner.setOnItemSelectedListener(new OnItemSelectedListener(){ }); but Android Studio "无法解析符号。

这是我的代码,如有任何帮助,我将不胜感激。我被卡住了,无法从这里继续前进!

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

    rootview = inflater.inflate(R.layout.fragment_deadline, container, false);
    super.onCreate(savedInstanceState);

    dates = new ArrayList<String>();
    registrationDates = new ArrayList<String>();
    deadlineDates = new ArrayList<String>();

    GetDeadlineDates getDeadlineDates = new GetDeadlineDates();
    getDeadlineDates.execute();

    deadlineDateText = (TextView) rootview.findViewById(R.id.deadlineDateText);

    deadlineSpinner = (Spinner) rootview.findViewById(R.id.deadlineSpinner);
    deadlineAdapter = new ArrayAdapter<String>
            (rootview.getContext(), android.R.layout.simple_spinner_item, registrationDates);
    deadlineAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    deadlineSpinner.setAdapter(deadlineAdapter);

    deadlineSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            int pos = deadlineSpinner.getSelectedItemPosition();
            deadlineAdapter.notifyDataSetChanged();
            deadlineDateText.setText(deadlineDates.get(pos));
        }

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

        }
    });

    final Button registrationButton = (Button) rootview.findViewById(R.id.registrationButton);
    registrationButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
        }

    });

    return rootview;
}

XML 微调器:

    <Spinner
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/deadlineSpinner"
    android:spinnerMode="dropdown"
    android:layout_marginTop="40dp"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_below="@+id/deadlineInfoText"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:clickable="false"
    android:gravity="center_horizontal"
    android:descendantFocusability="blocksDescendants"
    android:focusable="false" />

你为什么要找位置,android 给你自己的位置

 deadlineSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        //int pos = deadlineSpinner.getSelectedItemPosition();  //why find position here?
        deadlineAdapter.notifyDataSetChanged();
        deadlineDateText.setText(deadlineDates.get(position));
    }

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

    }
});