Someonation
Someonation

Reputation: 1

how to check if the user chose a choice in a spinner

I have a spinner that opens programaticly, and when the user chose an option from the spinner, it closes... is there a way to be notified, or a listener that tells you, when the user chose his choice? the onItemSelected gets the default item that is chosen automaticly when the spinner is open.

Upvotes: 0

Views: 461

Answers (3)

Samir Mangroliya
Samir Mangroliya

Reputation: 40416

set setOnItemSelectedListener to your spinner...

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

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

                    Object obj = (Object) parent.getSelectedItem();
            //get clicked position from position    
            }

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

            //this method is call when nothing choosed by you   

            }
        });

Upvotes: 1

binW
binW

Reputation: 13692

I am not sure if I understand your question correctly but let me try to answer it. The normal and straight forward way would be to add an OnItemSelectedListener to the spinner i.e

spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        // Do whatever you want here    
    }

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

but this is such a basic thing that I feel like a fool for pointing it out. Anyway here is a tutorial on spinner from Android Developer resources. It creates the listener in step 5 and adds it to spinner on step 6.

Upvotes: 0

nhaarman
nhaarman

Reputation: 100398

Add an OnItemSelectedListener to your Spinner.

Upvotes: 0

Related Questions