1

I have two spinners in a fragment, one of the spinner will display toast message when an item is selected. The problem is the toast in the first if-else statement displayed twice. Once when open the fragment and the second time when the item selected.

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

    mRootView = inflater.inflate(R.layout.add, container, false);



    //------------------------------unit Spinner adapter---------------------------------------//
    Spinner spinner = (Spinner) mRootView.findViewById(R.id.units);

    //Create ArrayAdapter using string array and default spinner
    ArrayAdapter<CharSequence> sAdapter = ArrayAdapter.createFromResource(getActivity(), R.array.units, android.R.layout.simple_spinner_dropdown_item);

    //Specify layout to use when list of choices appears
    sAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    //Apply adapter to the spinner
    spinner.setAdapter(sAdapter);


    //----------------------------reminder Spinner adapter-------------------------------------//
    Spinner reminderSpinner = (Spinner) mRootView.findViewById(R.id.list_reminder);
    ArrayAdapter<CharSequence> reminderAdapter = ArrayAdapter.createFromResource(getActivity(), R.array.countdown_reminder, android.R.layout.simple_spinner_dropdown_item);
    reminderAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    reminderSpinner.setAdapter(reminderAdapter);



    spinner.setOnItemSelectedListener(spinnerListener);
    reminderSpinner.setOnItemSelectedListener(spinnerListener);

return mRootView;
}


    //---------------------------------Spinner Listener----------------------------------------//
    AdapterView.OnItemSelectedListener spinnerListener = new AdapterView.OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            switch (parent.getId()) {

                case R.id.units:
                    String selectedUnit = parent.getItemAtPosition(position).toString();
                    break;
                case R.id.list_reminder:
                    String reminder = parent.getItemAtPosition(position).toString();


                    if (reminder.equals("24 hours")) {
                        Toast.makeText(getActivity(), "Reminder has been set 24 hours from the selected date", Toast.LENGTH_LONG).show();

                    } else if (reminder.equals("2 days")) {
                        Toast.makeText(getActivity(), "Reminder has been set 2 days from the selected date", Toast.LENGTH_LONG).show();

                    } else if (reminder.equals("3 days")) {
                        Toast.makeText(getActivity(), "Reminder has been set 3 days from the selected date", Toast.LENGTH_LONG).show();

                    } else if (reminder.equals("1 week")) {
                        Toast.makeText(getActivity(), "Reminder has been set 1 week from the selected date", Toast.LENGTH_LONG).show();

                    } else if (reminder.equals("2 weeks")) {
                        Toast.makeText(getActivity(), "Reminder has been set 2 weeks from the selected date", Toast.LENGTH_LONG).show();

                    } else if (reminder.equals("1 month")) {
                        Toast.makeText(getActivity(), "Reminder has been set 1 month from the selected date", Toast.LENGTH_LONG).show();
                    }
                    break;
            }
        }


        public void onNothingSelected(AdapterView<?> parent) {
        }
    };
    //------------------------------------end spinner code-------------------------------------//

The '24 hours' toast displayed when user open the fragment and when the '24 hours' selected. I can't figure out what's wrong with the code. Help please?!

Joan Z
  • 43
  • 1
  • 9
  • http://stackoverflow.com/questions/5624825/spinner-onitemselected-executes-when-it-is-not-suppose-to/5918177#5918177 – Aaron He Nov 21 '15 at 15:39
  • Thanks for the link. I tried the solution from the link and it works! Thank you. – Joan Z Nov 22 '15 at 09:27

3 Answers3

3

Your onItemSelectedListener might be getting called when you set it in onCreateView or it could be getting called when it initially displays the view. One hacky work-around is just to have a boolean that only executes the code in your listener after it has been called at least once before.

Andrew Orobator
  • 7,978
  • 3
  • 36
  • 36
2

IIRC, a spinner must have a selected item no matter what, so it sets the first item when it launches and thus OnItemSelected is fired when the spinner starts up. No way around it unless you want to hack the spinner code.

What you could do is add a counter variable to determine whether or not it is the first time through and not display the toast if it is the first time.

Barak
  • 16,318
  • 9
  • 52
  • 84
1

This is because your adpater's onItemSelected is getting called when you are setting the adapter to your spinner. Add a debug point in that if block. A soluion would be to use a boolean flag to check whether the user has selected the spinner or not. Initially make it false. then before your switch check if that flag is false. If it is false then don't perform switch else do it. make the boolean flag true after the switch case. Hence it will be false for the first load and the toast will not appear for when the fragment is loaded.

Bhushan
  • 205
  • 2
  • 14