tags:

views:

24

answers:

1

It appears that android's Spinner class (and possibly ListView in general, although I don't know for sure) calls your OnItemSelectedListener's onItemSelected() method after you call setAdapter(), even if the user hasn't explicitly selected anything yet.

I can see how this would be useful in many situations, but there are times when I only want onItemSelected() to be called when an item is actually specifically selected.

Is there a way to control this behavior and have Spinner NOT call onItemSelected() after setting the adapter?

A: 

I haven't used this solution for very long yet so I'm not totally confident that it works as expected, but I've had luck so far with this workaround:

    spinner.setOnItemSelectedListener( new OnItemSelectedListener() {
        protected Adapter initializedAdapter = null;

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

            // Always ignore the initial selection performed after setAdapter
            if( initializedAdapter !=parent.getAdapter() ) {
                initializedAdapter = parent.getAdapter();
                return;
            }

            ...
        }
    }

Is there a better way?

Mike