Anish
Anish

Reputation: 166

how to know if user clicked spinner android

I have implemented multiple(seven) spinners and populated them with three options: Yes, No, and Unknown. And "Unknown" is the default option. Now I want to know whether user clicked spinner or not. Since the default option can also be a valid answer, I could not work with getSelectedItemPosition() in Spinner class.

All I want to know is whether user clicked that particular spinner or not, so that I can generate alert message depending on this Info.

Upvotes: 1

Views: 1688

Answers (2)

Hardik Nadiyapara
Hardik Nadiyapara

Reputation: 2436

Simpy, First Set OnItemSelectedListener to each Spinner and then check in method,

if you have more spinner then getSelectedItem() using below code inside onItemSeleted Method,

String str1= (String) spinner1.getSelectedItem().toString();
String str2= (String) spinner2.getSelectedItem().toString();

Upvotes: 0

twaddington
twaddington

Reputation: 11645

The first thing you should do is read the Spinners guide on the Android developer site. Having done that, you'll find this handy example:

public class MySpinnerActivity extends Activity implements OnItemSelectedListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        spinner.setOnItemSelectedListener(this);
    }

    public void onItemSelected(AdapterView<?> parent, View view, 
            int pos, long id) {
        // An item was selected. You can retrieve the selected item using
        // parent.getItemAtPosition(pos)
    }

    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }
}

Upvotes: 1

Related Questions