46

Possible Duplicate:
Android Spinner OnItemSelected Called Erroneously (without user action on opening spinner)

Does anyone know how to prevent the onItemSelected() (OnItemSelectedListener interface) method from running when the layout is instantiated? I need to know if there is a way to do this because I want to keep how I instantiate my layout separate from this listener.

I have tried creating an if statement initially set to false around all the code inside of the overridden method, but there is no way of knowing when to set it to true because the overridden method runs after the onCreate(), onStart(), and onResume() methods everytime.

I have not found any clear cut answers on this. Any clear cut solutions would be greatly appreciated.

Andrew Brēza
  • 7,705
  • 3
  • 34
  • 40
David
  • 544
  • 1
  • 6
  • 12
  • 8
    The way spinner is designed makes me sick... Definitely worst UI control i've ever worked with... – hendrix Dec 07 '12 at 18:07
  • 1
    Should be marked as a duplicate of http://stackoverflow.com/questions/2562248/how-to-keep-onitemselected-from-firing-off-on-a-newly-instantiated-spinner instead. – Jonik Aug 26 '14 at 15:36
  • You can find answer, I have answered here http://stackoverflow.com/questions/10132971/how-to-make-on-item-selected-not-automatically-choose-the-first-entry/28062775#28062775 – karan Jan 21 '15 at 08:39

6 Answers6

66

David, here is a tutorial I wrote up for this problem...

Problem Statement

an undesirable onItemSelected() is triggered whilst the Gallery (or Spinner) is initializing. This means that code is prematurely executed; code which is intended to execute ONLY when a user physically makes a selection.

Solution

  1. in onCreate(), count how many Gallery (or Spinner) widgets you have in the view. (mGalleryCount)
  2. in onItemSelected(), count how often it has triggered. (mGalleryInitializedCount)
  3. when (mGalleryInitializedCount < mGalleryCount) == false, then execute the code meant for the user

Code Example

public class myActivity extends Activity implements OnItemSelectedListener
{
    //this counts how many Gallery's are on the UI
    private int mGalleryCount=0;

    //this counts how many Gallery's have been initialized
    private int mGalleryInitializedCount=0;

    //UI reference
    private Gallery mGallery;


    @Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.myxmllayout);

        //get references to UI components
        mGallery = (Gallery) findViewById(R.id.mygallery);

        //trap selection events from gallery
        mGallery.setOnItemSelectedListener(this);

        //trap only selection when no flinging is taking place
        mGallery.setCallbackDuringFling(false);

        //
        //do other stuff like load images, setAdapter(), etc
        //

        //define how many Gallery's are in this view
        //note: this could be counted dynamically if you are programmatically creating the view
        mGalleryCount=1;

    }


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

        if (mGalleryInitializedCount < mGalleryCount)
        {
            mGalleryInitializedCount++;
        }
        else
        {
            //only detect selection events that are not done whilst initializing
            Log.i(TAG, "selected item position = " + String.valueOf(position) );
        }

    }

}

Why this works

this solution works because the Gallery finishes initialization long before a user is physically able to make a selection.

Someone Somewhere
  • 23,475
  • 11
  • 118
  • 166
  • 1
    This is a good answer, as well as the boolean variant khr2003 gave for a single view. Looks like the loading of a view layout and the items executes the itemselected. I find this to be a bug since the actionword selected indicates user interaction and not initialisation setup. – EeKay May 03 '12 at 08:24
  • Thanks. This fixed a silly problem. :) – IAmGroot Jul 03 '12 at 12:24
  • Why do I get an error on this line: mSpinner.setCallbackDuringFling(false); saying: The method setCallbackDuringFling(boolean) is undefined for the type Spinner? Please help, After hours I finally thought I had found a fix for this anoying problem. – Jakob Harteg Nov 18 '12 at 22:12
  • Harteg, I check the Spinner class and you're right, the method doesn't exist for that class. The spirit of this solution should still apply, but otherwise you can probably just leave that line of code out. – Someone Somewhere Nov 21 '12 at 22:14
  • 2
    +1 for the answer. Just a comment that variable `mSpinnerCount` and `mSpinnerInitializedCount` could be more precise. I was lost a bit by `Gallery`, though you mentioned Spinner in the brackets :) – PCoder Feb 06 '13 at 13:34
  • Thanks! I was going crazy with this thinking I was doing something wrong. – eskalera Jan 14 '15 at 12:21
  • An alternate solution is to set your onSelectedListener in a posted runnable, ie. spinner.postRunnable(//insert runnable that sets your items selected listener); This way it will place it on the spinner message queue and will run it once the spinner is properly initialized. See more about runnables here: http://stackoverflow.com/questions/13840007/what-exactly-does-the-post-method-do – Jawnnypoo Jan 15 '15 at 21:08
  • This works fine, but user is not able to select the item itself. But did I mistake? in this mGalleryCount=0; I put the count for all items to load in spinner and mGalleryInitializedCount ramains as 0. and Spinner items is not getting selected? – Deepak Feb 04 '16 at 07:31
15

Here is a modified version of "Someone Somewhere" code. You can use it if you have a single view.

public class myActivity extends Activity implements OnItemSelectedListener
{
// Set view initialization to false while the it is being built
private boolean initializedView = false;

//UI reference
private Gallery mGallery;


@Override
public void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.myxmllayout);

    //get references to UI components
    mGallery = (Gallery) findViewById(R.id.mygallery);

    //trap selection events from gallery
    mGallery.setOnItemSelectedListener(this);

    //trap only selection when no flinging is taking place
    mGallery.setCallbackDuringFling(false);

    //
    //do other stuff like load images, setAdapter(), etc
    //


}


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

    if (initializedView ==  false)
    {
        initializedView = true;
    }
    else
    {
        //only detect selection events that are not done whilst initializing
        Log.i(TAG, "selected item position = " + String.valueOf(position) );
    }

}
}
Kalimah
  • 11,217
  • 11
  • 43
  • 80
11

Same solution:

private int m_intSpinnerInitiCount = 0;
private static final int NO_OF_EVENTS = 1;

...

m_spnTemplates.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, 
                    View selectedItemView, int position, long id) { 

                //trying to avoid undesired spinner selection changed event, a known problem
                    if (m_intSpinnerInitiCount < NO_OF_EVENTS) {
                        m_intSpinnerInitiCount++;
                    } else {                
                        //YOUR CODE HERE
                    }                           
            }
Dimitar Vukman
  • 421
  • 6
  • 15
1

I ran into this problem yesterday with an OnCheckedChangedListener. I ended up adding a boolean instance variable initialized to true inside of my adapter class with an accessor method isListenerEnabled(). I then set the variable to false in my layout code and set it to true again at the end of the layout code. In my listener, I inspect the value of the variable to decide whether to execute the listener code or not.

James
  • 2,346
  • 1
  • 16
  • 18
1

You can set your variable back to false everytime on onPause is called. As for when to set it to true, you could do this for the first motion/key/trackball event after onResume.

Joseph Earl
  • 23,351
  • 11
  • 76
  • 89
  • A slight bodge I found is: if (selectedItem.contains("Select Postcode...")==false) { Log.d("Process","Process selected item"); } So long as the spinner has the text "Select Postcode..." or whatever it works... – Ian Jul 12 '12 at 15:19
1

I also looked for a good solution on the internet but didn't find any that satisfied my needs. So I've written this extension on the Spinner class so you can set a simple OnItemClickListener, which has the same behaviour as a ListView.

Only when an item gets 'selected', the onItemClickListener is called.

Have fun with it!

public class MySpinner extends Spinner
{
    private OnItemClickListener onItemClickListener;


    public MySpinner(Context context)
    {
        super(context);
    }

    public MySpinner(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public MySpinner(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    @Override
    public void setOnItemClickListener(android.widget.AdapterView.OnItemClickListener inOnItemClickListener)
    {
        this.onItemClickListener = inOnItemClickListener;
    }

    @Override
    public void onClick(DialogInterface dialog, int which)
    {
        super.onClick(dialog, which);

        if (this.onItemClickListener != null)
        {
            this.onItemClickListener.onItemClick(this, this.getSelectedView(), which, this.getSelectedItemId());
        }
    }
}
Cliffus
  • 1,481
  • 2
  • 11
  • 7