Spinner item gets automatically selected upon entering activity. How do I avoid this?
Asked Answered
M

9

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?

Melton answered 20/3, 2011 at 11:35 Comment(0)
C
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 
                    }
        });
Chummy answered 25/6, 2014 at 3:27 Comment(3)
How would isSpinnerTouched be set to false once the user has finished touching the spinner?Petulah
@Teifi, I guess set the isSpinnerTouched to false in onItemSelected after the operation that you need has done....Drift
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.Drift
C
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
Comatulid answered 28/8, 2017 at 20:5 Comment(0)
H
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()

Heroism answered 21/7, 2013 at 12:6 Comment(2)
second argument of setSelection has nothing to do with onItemSelected()Laurelaureano
But you need to set it in order 1) get a view 2) set adapter 3) set default value 4) last add on select event listenerProsecution
P
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.

Poetaster answered 20/3, 2011 at 11:38 Comment(0)
W
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");
        }
    }
Walking answered 10/3, 2014 at 18:11 Comment(0)
B
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

Beaner answered 7/5, 2011 at 0:15 Comment(0)
U
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

Unconformity answered 11/1, 2017 at 1:5 Comment(0)
P
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) {

            }
        });
Peppard answered 23/2, 2021 at 14:51 Comment(1)
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 oneBoehmer
Z
-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
}
Zaibatsu answered 22/1, 2019 at 13:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.