Spinner不选择项目

时间:2015-04-28 09:33:37

标签: android android-fragments android-studio spinner android-arrayadapter

我已经使用了很长时间而且我无法让它工作......我有一个微调器,其中包含了我班级中延伸片段的日期列表。但是,当我从下拉菜单中选择一个日期时,没有任何反应。它应该更改文本视图的文本并在微调器中显示所选项...

我一直在阅读Spinners的其他例子,有些人说它不能点击但是我无法打开Spinner ......

我也看到过要传递的例子"这个"在创建适配器时,我开始收到一条错误,指出"无法解析构造函数......"

其他人说使用" spinner.setOnItemSelectedListener(new OnItemSelectedListener(){});但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;
}

Spinner的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" />

1 个答案:

答案 0 :(得分:0)

为什么你找到位置,当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) {

    }
});