Freestyle076
Freestyle076

Reputation: 1556

Creating multiple onItemSelectedListeners

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!

Upvotes: 4

Views: 9672

Answers (5)

Walter Jamison
Walter Jamison

Reputation: 36

Use this example for using multiple onItemSelectedListeners

OnItemSelectedListener myListener=new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
             switch (arg2) {
                case 1:
                    Toast.makeText(SpinnerActivity.this,"Spinner 1", Toast.LENGTH_LONG).show();
                    break;
                case 2:
                    Toast.makeText(SpinnerActivity.this, "Spinner 2", Toast.LENGTH_LONG).show();         
                    break;
                case 3:
                    Toast.makeText(SpinnerActivity.this, "Spinner 2", Toast.LENGTH_LONG).show();
                    break;
                case 4:
                    Toast.makeText(SpinnerActivity.this,"Spinner 2", Toast.LENGTH_LONG).show();
                    break;
                case 5:
                    Toast.makeText(SpinnerActivity.this, "Spinner 2", Toast.LENGTH_LONG).show();
                    break;
                }

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    };

Upvotes: 0

user2652394
user2652394

Reputation: 1686

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.

Upvotes: 19

Bhavna
Bhavna

Reputation: 856

You can set same onItemSelected listener for all the Spinners.. like

spinner1.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);

and the implement the action in single method using the id of the spinners

@Override
    public void onItemSelected(AdapterView<?> spinner, View view,
                                                  int position,long arg3) 
    {
    int id = spinner.getId();  //You can also use int id= view.getId();
    switch (id) 
    {
            case R.id.spinner1:
            // Do what you want
            break;
        case R.id.spinner2:
           // Your another task
        break;

    }

Upvotes: 2

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

Use anonymous interface for more readability

spin1.setOnItemSelectedListener(new OnItemSelectedListener() {

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

            }

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

            }
        });

        spin2.setOnItemSelectedListener(new OnItemSelectedListener() {

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

            }

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

            }
        });

Upvotes: 0

Pork &#39;n&#39; Bunny
Pork &#39;n&#39; Bunny

Reputation: 6731

The function call includes which adapterview is responsible for making the call.

So just check which adapterview called your function and have code handling that respective adapter.

Upvotes: 0

Related Questions