I'm working on creating an android activity that has two spinners in it. I understand how to implement the onItemSelectedListener
for one spinner, using the onItemSelected
call back function:
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
Spinner spinner = (Spinner) findViewById(R.id.spinnerOneOfTwo);
spinner.setOnItemSelectedListener(this);
//do things with selection...
}
However, what if I have multiple spinners? It seems to me that I would need a separate function, but since both spinners are set to call back onItemSelected()
I can't take that approach. Is there any way to tell which spinner is calling the onItemSelected()
function? Perhaps one of the parameters keys on which spinner is making the call? Then I could set its ID as the parameter for my spinner variable's ID?
I know there is a way (I'm definitely not the only one putting multiple spinners in one activity), any hints are much appreciated!
Let 2 of your Spinners implements the same OnItemSelectedListener
and try this:
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
switch (parent.getId()) {
case R.id.your_spinner_1_id:
// do stuffs with you spinner 1
break;
case R.id.your_spinner_2_id:
// do stuffs with you spinner 2
break;
default:
break;
}
}
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With