Braulio
Braulio

Reputation: 17

Spinner does not select item

I've been on this for a very long time and I can't get it to work... I have a spinner that is populated by a list of dates in my class that extends Fragment. However, when I select one of the dates from the dropdown spinner nothing happens. It should change the text of a textview and show the selected item in the spinner...

I've been reading other examples of Spinners and some say to have it not clickable but then I can't open the Spinner...

I've also seen examples say to pass in "this" when creating the adapter but I begin to get an error that says "cannot resolve constructor..."

Others say to use "spinner.setOnItemSelectedListener(new OnItemSelectedListener(){ }); but Android Studio "cannot resolve the symbol".

Here's my code and I would appreciate ANY help. I'm stuck and can't move on from here!

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 for Spinner:

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

Upvotes: 0

Views: 7942

Answers (1)

Neal Ahluvalia
Neal Ahluvalia

Reputation: 1548

Why are you finding position, when android gives you position on its own

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

    }
});

Upvotes: 2

Related Questions