Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Hard to believe that a year and a half later, the problem still exists and continues to boggle people...</p> <p>Thought I'd share the workaround I came up with after reading Arhimed's most useful post (thanks, and I agree about spinners being painful!). What I've been doing to avoid these false positives is to use a simple wrapper class:</p> <pre><code>import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; public class OnItemSelectedListenerWrapper implements OnItemSelectedListener { private int lastPosition; private OnItemSelectedListener listener; public OnItemSelectedListenerWrapper(OnItemSelectedListener aListener) { lastPosition = 0; listener = aListener; } @Override public void onItemSelected(AdapterView&lt;?&gt; aParentView, View aView, int aPosition, long anId) { if (lastPosition == aPosition) { Log.d(getClass().getName(), "Ignoring onItemSelected for same position: " + aPosition); } else { Log.d(getClass().getName(), "Passing on onItemSelected for different position: " + aPosition); listener.onItemSelected(aParentView, aView, aPosition, anId); } lastPosition = aPosition; } @Override public void onNothingSelected(AdapterView&lt;?&gt; aParentView) { listener.onNothingSelected(aParentView); } } </code></pre> <p>All it does is trap item selected events for the same position that was already selected (e.g. the initial automatically triggered selection for position 0), and pass on other events to the wrapped listener. To use it, all you have to do is modify the line in your code that calls the listener to include the wrapper (and add the closing bracket of course), so instead of, say:</p> <pre><code>mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() { ... }); </code></pre> <p>you'd have this:</p> <pre><code>mySpinner.setOnItemSelectedListener(new OnItemSelectedListenerWrapper(new OnItemSelectedListener() { ... })); </code></pre> <p>Obviously once you've tested it, you could get rid of the Log calls, and you could add the ability to reset the last position if required (you'd have to keep a reference to the instance, of course, rather than declaring on-the-fly) as Arhimed said.</p> <p>Hope this can help someone from being driven crazy by this strange behaviour ;-)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload