Spinner Onitemselected() Executes Inappropriately

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 called erroneously (without user action)

Androider, I have found a solution for this problem and posted it here (with code sample):

Spinner onItemSelected() executes when it is not suppose to

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 catch fire of onItemSelected of a Spinner, after the interface has been setup?

Have you tried waiting until you have populated the Spinner to register the onItemSelectedListener?

setOnItemSelectedListener makes my app crash

Exception clearly states the following:

Receiver not registered: com.example.javachat.MainActivity$1@418afa30

Crash happens here:

03-24 19:48:32.967: E/AndroidRuntime(23058):at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:445)
03-24 19:48:32.967: E/AndroidRuntime(23058): at com.example.javachat.MainActivity.onDestroy(MainActivity.java:229)
03-24 19:48:32.967: E/AndroidRuntime(23058):at com.example.javachat.MainActivity.onCreate(MainActivity.java:136)

You have unregisterReceiver at the onDestroy, but you have no place, where you are registering it. Also you would like to take a look here, because onItemSelected will be called for Spinner without user action.

But it is not the end. The BroadcastReceiver declaration is wrong. Since you are using broadcast receiver locally, binding it to the Activity lifecycle, you should use LocalBroadcastManager, to register/unregister at the onCreate/onDestroy (or other lifecycle callbacks, depends on your needs). Thus, the following declaration in manifest makes no sense:

 <receiver android:name=".bReceiver">
</receiver>

Because you have no class called bReceiver, you have a local variable (which should be actually made a class member) bReceiver, which points to an instance of BroadcastReceiver class.

Android Spinner: app crashes onItemSelected

May be ((TextView) view.findViewById(R.id.movieName)).getText().toString(); is null. Take your String from ListViewValues

SpinnerExample.setOnItemSelectedEvenIfUnchangedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// i++; //Avoid fist time oppening the OnClick start new intent. First time is for set pic of the first element in the list.
String Movie = (String) CustomListViewValuesArr1.get(postion)
// if(i>=1){
Intent intent = new Intent(CustomSpinner.this, MovieTrailer.class);
if(position==0) {
intent.putExtra("Cartoons","10");
} else if(position==1) {
intent.putExtra("Cartoons","11");
} else if(position==2) {
intent.putExtra("Cartoons","12");
} else if(position==3) {
intent.putExtra("Cartoons","13");
} else if(position==4) {
intent.putExtra("Cartoons","14");
} else if(position==5) {
intent.putExtra("Cartoons","15");
} else if(position==6) {
intent.putExtra("Cartoons","16");
} else if(position==7) {
intent.putExtra("Cartoons","17");
} else if(position==8) {
intent.putExtra("Cartoons","18");
}
startActivity(intent);
//}
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});

How to make On Item Selected not automatically choose the first entry

Does anyone know a way in which the first item on the list wont be automatically selected?

There is always a selection on Spinner, and you cannot change that.

IMHO, you should not be using a Spinner to trigger starting an activity.

That being said, you can use a boolean to track whether this is the first selection event, and ignore it if it is.

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.



Related Topics



Leave a reply



Submit