Android Multiple Spinners in 1 class

80 views Asked by At

I am trying to create 2 spinners on a single page. My code is below for onItemSelected;

@Override

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3)              {

    switch(arg0.getId()) {
    case R.id.spinner1:

        switch(arg2){

        case 0:
        Toast.makeText(this,  "THIS IS SPINNER 1" ,  Toast.LENGTH_SHORT).show();
        break;
        case 1:
        Intent intent1 = new Intent(this, Screen2.class);
        startActivity(intent1);
        break;
        case 2:
        Intent intent2 = new Intent(this, Screen2.class);
        startActivity(intent2);
        break;}

    case R.id.spinner2:

        switch(arg2){
        case 0:
        Toast.makeText(this,  "THIS IS SPINNER 2" ,  Toast.LENGTH_SHORT).show();
        break;
        case 1:
        Intent intent1 = new Intent(this, Screen3.class);
        startActivity(intent1);
        break;
        case 2:
        Intent intent2 = new Intent(this, Screen3.class);
        startActivity(intent2);
        break;
        }
    }
    }

But when I run the program, the cases are not responding as I expect, eg spinner 2 sends it to screen3 when it should go to screen2

1

There are 1 answers

0
AudioBubble On

I found the answer myself, it is as follows:

int a = arg0.getId();

    if (a==R.id.spinner1){
        switch(arg2) {

//Do something
                                 }

    if (a==R.id.spinner2){
        switch(arg2) {

//Do something
                                 }