How to avoid using the android spinner element select function when configuring the adapter?

It looks like the Android Spinner class (and possibly the ListView as a whole, although I don’t know for sure) calls your OnItemSelectedListener onItemSelected() method after calling setAdapter() , even if the user has not explicitly selected anything yet.

I see how this would be useful in many situations, but there are times when I want onItemSelected() called when the item is really specifically selected.

Is there a way to control this behavior even after you have installed the adapter using the Spinner function NOT call onItemSelected() ?

+6
android android spinner
source share
5 answers

I have not used this solution for a very long time, but I'm not sure if it works as expected, but so far I have been lucky 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?

+4
source share

Add a listener to the spinner as shown below:

 spinner.post(new Runnable(){ public void run() { spinner.setOnItemSelectedListener( new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { ... } } } }); 
+2
source share

I used setTag and getTag methods and created a resource id called "spinnerstate".

Then whenever I install the adapter programmatically, I set the spinnerstate tag to init, and in the fired event, set it to ready and ignore the event. (note that my code is Mono for Android, it will look different):

Install adapter:

 profileSpn.SetTag (Resource.Id.spinnerstate, "init"); profileSpn.Adapter = new ArrayAdapter (this, Android.Resource.Layout.SimpleSpinnerItem, items.ToArray ()); 

Element Selected Event:

  string state = (string)((Spinner)sender).GetTag (Resource.Id.spinnerstate); if (state == "init") { ((Spinner)sender).SetTag (Resource.Id.spinnerstate, "ready"); return; } 

I agree that this is undesirable behavior in almost 100% of cases, and I do not think that this is a good design by Google, but there you go.

0
source share

I did similar things before, I used count value. Using the parent adapter object is incomplete, as this may be a problem when updating the view or getView() .

Therefore, I recommend using an array of counters.

First, define the count array in the adapter globally.

 private int isInitializedView[]; 

And then initialize it to getView ().

 isInitializedView[position] = 0; 

In the selection listener, do what you want if it is already initialized.

  holder.mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { isInitializedView[position]++; if(isInitializedView[position] > 1) { // do someting that you want } } @Override public void onNothingSelected(AdapterView<?> parentView) {} }); 

(Note that isInitializedView[position]++; may appear after the if() procedure and only trigger event when this value >0 This is your choice.)

0
source share

I had three counters in my activity, and all of the counter adapter data was filled at runtime (from the web service data after calling the onCreate method). Therefore, it automatically calls the onItemSelected(AdapterView<?> parent, View view, int position, long id) method of the counter. I solved this problem using the onUserInteraction () activity method, check this method with which the user interacts with the counter or not. if so, do the else action

  • Declare isUserIntract boolean variable globally

  • in onItemSelected use the following procedure

    If (isUserIntract) {

    // perform an action

    } still {

    // do not perform an action

    }

  • use the code below in action

@Override

 public void onUserInteraction() { super.onUserInteraction(); isUserIntract = true; } 
0
source share

All Articles