Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Spinner - onItemSelected / setOnItemSelectedListener not triggering

This is driving me nuts since it's something I've done before but can't figure out why it isn't working now...

I've got a menu button, implemented in the usual way via a menu.xml file and the onOptionsItemSelected method with a switch in it, that creates and displays a spinner.

I've added the setOnItemSelectedListener, but it never seems to trigger. The spinner appears, I pick an option or back out, neither onItemSelected or onNothingSelected are called.

Here is all the code between the "case" and "return true" of the menu-button-handling switch statement. (topThis is a variable referring to the context of the activity - works fine for all other toasts in the app)

String[] widgetModes = {"Mode 1", "Mode2"};
ArrayAdapter<String> widgetModeAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, widgetModes);
widgetModeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

Spinner widgetModeSpinner = new Spinner(this);
widgetModeSpinner.setAdapter(widgetModeAdapter);
widgetModeSpinner.setPrompt("Choose Widget Mode");

widgetModeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() 
{
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) 
    {
        Toast.makeText(topThis, "derp", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) 
    {
        Toast.makeText(topThis, "herf", Toast.LENGTH_LONG).show();
    }
});

widgetModeSpinner.performClick();

Any ideas? I vaguely suspect that the fact I'm creating the Spinner programmatically is the problem...

like image 527
Greg Avatar asked Feb 07 '11 15:02

Greg


2 Answers

I had the similar problem when I was implementing a spinner, I resolved it by getting the parent View and set Adapter-

spinner1 =(Spinner)findViewById(R.id.spinner1);
spinner1.setAdapter(BindSpinner("ProgramMaster",cols,null,true,""));
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() 
{
protected Adapter initializedAdapter=null;
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) 
    {
        if(initializedAdapter !=parentView.getAdapter() ) {
            initializedAdapter = parentView.getAdapter();
           return;
        }

        String selected = parentView.getItemAtPosition(position).toString();

        if(abc.equals("Select") && !selected.equals("Select"))
        {
            do something
        }

        else 
        {
            Do something
        }
        textQualification=selected;
        SearchUpdated("Qualification");
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) {
        // your code here
    }
});
like image 94
JNI_OnLoad Avatar answered Sep 28 '22 09:09

JNI_OnLoad


Remember that you can't re-select the same spinner item, it always sets the first item as selected if you are not adding some custom code to handle the spinner selection.

For the Toast not showing, I would suggest to always use the "MyActivity.this" as your context when creating a Toast inside a listener interface like this:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        /**
         * Called when a new item is selected (in the Spinner)
         */
         public void onItemSelected(AdapterView<?> parent, View view, 
                    int pos, long id) {
                // An spinnerItem was selected. You can retrieve the selected item using
                // parent.getItemAtPosition(pos)

                Toast.makeText(MyActivity.this, "Hello Toast",Toast.LENGTH_SHORT).show();

            }

            public void onNothingSelected(AdapterView<?> parent) {
                // Do nothing, just another required interface callback
            }

    }); // (optional)

And the .show() at the end is easy to forget sometimes;)

like image 22
TouchBoarder Avatar answered Sep 28 '22 08:09

TouchBoarder