How to Keep Onitemselected from Firing Off on a Newly Instantiated Spinner

How to keep onItemSelected from firing off on a newly instantiated Spinner?

I would have expected your solution to work -- I though the selection event would not fire if you set the adapter before setting up the listener.

That being said, a simple boolean flag would allow you to detect the rogue first selection event and ignore it.

How to keep onItemSelected from firing off on a newly instantiated Spinner?

I would have expected your solution to work -- I though the selection event would not fire if you set the adapter before setting up the listener.

That being said, a simple boolean flag would allow you to detect the rogue first selection event and ignore it.

Android Spinner : Avoid onItemSelected calls during initialization

spinner.setOnItemSelectedListener(this); // Will call onItemSelected() Listener.

So first time handle this with any Integer value

Example:
Initially Take int check = 0;

public void onItemSelected(AdapterView<?> parent, View arg1, int pos,long id) {
if(++check > 1) {
TextView textView = (TextView) findViewById(R.id.textView1);
String str = (String) parent.getItemAtPosition(pos);
textView.setText(str);
}
}

You can do it with boolean value and also by checking current and previous positions. See here

prevent spinner,s OnItemSelectedListner being fired on startup

this might be able to help Android Spinner selection

Spinner onItemSelected() executes inappropriately

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.

Spinner onItemSelected keeping called over and over again

OnItemSelected is always called when the adapter is first set. AFAIK, there is no way to stop this from happening. What you can do is set a boolean flag in your Activity and set it to true in your listener after it runs through the first time and don't run the code inside when it is false.

Using onFocusChangeListener could allow you to listen for the Spinner to get focus and do or don't do something when it gets focus. But I still think simply using a flag would be the simplest



Related Topics



Leave a reply



Submit