21

I have a spinner in my Android app, and its onItemSelected() event automatically gets triggered upon entering the activity.

How do I avoid this?

Jonik
  • 80,077
  • 70
  • 264
  • 372
user594720
  • 449
  • 3
  • 9
  • 18

9 Answers9

12

We can use a flag, and just enable it when the spinner is really touched.

private boolean isSpinnerTouched = false; 

spinner.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                isSpinnerTouched = true;
                return false;
            }
        });
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapter, View arg1,
                        int arg2, long arg3) {
                    if (!isSpinnerTouched) return;
                    // do what you want 
                    }
        });
asclepix
  • 7,971
  • 3
  • 31
  • 41
thanhbinh84
  • 17,876
  • 6
  • 62
  • 69
  • How would isSpinnerTouched be set to false once the user has finished touching the spinner? – Mike Baxter Dec 14 '15 at 16:16
  • @Teifi, I guess set the `isSpinnerTouched` to false in `onItemSelected` after the operation that you need has done.... – Opiatefuchs Jan 29 '17 at 10:21
  • there is another problem, which suggests this answer is false: The touch listener consumes the event and `onItemSelected` is not called. No omatter if `onTouch` returns true or false. – Opiatefuchs Jan 29 '17 at 10:28
9

To add on Jerry Abraham, You should clear selection before enabling setOnItemSelectedListener

  Spinner mSpinner=(Spinner)findViewById(R.id.mySpinner);
  int initialSelectedPosition=mSpinner.getSelectedItemPosition();
  mSpinner.setSelection(initialSelectedPosition, false); //clear selection
  mSpinner.setOnItemSelectedListener(this); //set listener after clearing section
Charden Daxicen
  • 405
  • 5
  • 10
8

I have solved this issue, You can avoid this issue by not setting any default values to the spinner

        int initialposition=spinner.getSelectedItemPosition();
        spinner.setSelection(initialposition, false);

This will avoid entering into onItemSelected()

Ronak Gadhia
  • 524
  • 5
  • 12
Jerry Abraham
  • 1,039
  • 18
  • 42
5

There are no any way to avoid this.

You may add some flag, indicating readiness of your application and use it in your onItemSelected() method to decide, what to do in each case.

Olegas
  • 10,349
  • 8
  • 51
  • 72
1

Well, you can add a dummy selection to the initial adapter, and ignore position number in the setOnItemSelectedListener. It's not pretty but it works. See this code for setting up the items for an array adapter.

List<String> names = new ArrayList<String>();
names.add("");
names.addAll(realValues);

Then in your setOnItemSelectedListener you can do this:

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
    {
        if (position > 0)
        {
            String name = names.get(position - 1);
        }
        else
        {
            Log.d(TAG, "selected nothing or perhaps the dummy value");
        }
    }
gregm
  • 12,019
  • 7
  • 56
  • 78
0

Simple and easy is this... validate with a boolean to see if is first time...

Spinner mySpinner = (Spinner)findViewById(R.id.spinner_xml_pro);
        mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if(isSpinnerInitial){ // globar var boolean isSpinnerInitial = false;
                 //do something
               }else
                    isSpinnerInitial=true;
            }

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

            }
        });

Check this with spinner.post(new Runnable()...) or this other my source

Community
  • 1
  • 1
DarckBlezzer
  • 4,578
  • 1
  • 41
  • 51
0

I have found a solution for this problem and posted it here (with code sample):

Spinner onItemSelected() executes when it is not suppose to

Community
  • 1
  • 1
Someone Somewhere
  • 23,475
  • 11
  • 118
  • 166
0

I think that you can use spinner position which is a better approach in my opinion. Create a global variable where you store the spinner position, in onItemSelected method the position is provided you can compare them, if they are the same do not make an action.

    private int spinnerPosition; \\ Global variable

     mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
               if(spinnerPosition != position){
               // Do whatever you like

               // Do not forget to save the new position
                spinnerPosition = position;
               }

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

            }
        });
هيثم
  • 791
  • 8
  • 11
  • it wont help cuz every time in creating view ( entering the activity as he said ) the spinner adapter was set and new position will be the first one – Mostafa Imani Mar 28 '22 at 15:00
-2

You can avoid it by ignoring the first click by,

private boolean isSpinnerInitial = true; //As global variable

public void onItemSelected(xxx xxx, xxx xxx, xxx xxx, xxx xxx) {
    if(isSpinnerInitial) {
        isSpinnerInitial = false;
        return;
    }
    // Write your code here
}
Syed Zeeshan
  • 490
  • 1
  • 7
  • 13