Friday, April 27, 2012

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


I've thought of some less than elegant ways to solve this, but I know I must be missing something.



My onItemSelected fires off immediately without any interaction with the user, and this is undesired behavior. I wish for the UI to wait until the user selects something before it does anything.



I even tried setting up the listener in the onResume, hoping that would help, but it doesn't.



How can I stop this from firing off before the user can touch the control? THANKS




public class CMSHome extends Activity {

private Spinner spinner;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Heres my spinner ///////////////////////////////////////////
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.pm_list, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
};

public void onResume() {
super.onResume();
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}

public class MyOnItemSelectedListener implements OnItemSelectedListener {

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

Intent i = new Intent(CMSHome.this, ListProjects.class);
i.putExtra("bEmpID", parent.getItemAtPosition(pos).toString());
startActivity(i);

Toast.makeText(parent.getContext(), "The pm is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}

public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}



}


Source: Tips4all

6 comments:

  1. 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.

    ReplyDelete
  2. this solved my problem so maybe someone could find it useful too ;-)

    Android Spinner selection

    ReplyDelete
  3. I have found a solution for this problem and posted it here (with code sample):

    Spinner onItemSelected() executes when it is not suppose to.

    ReplyDelete
  4. I would try to call

    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());


    after you call setAdapter(). Also try out calling before the adapter.

    You always have the solution to go with subclassing, where you can wrap a boolean flag to your overriden setAdapter method to skip the event.

    ReplyDelete
  5. Referring to the answer of Dan Dyer try to register the OnSelectListener in a post(Runnbale) method:

    spinner.post(new Runnable() {
    public void run() {
    spinner.setOnItemSelectedListener(listener);
    }
    });


    By doing that for me the wished behavoir finally occurred.
    In this case it also means that the listener only fires on a changed item.

    ReplyDelete
  6. You can look at this solution, it is easy and practical.

    http://stackoverflow.com/a/10102356/621951

    ReplyDelete