Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple spinner's simple doubts

Tags:

android

Im new to android. i have tried to built an application which has 2 spinner... i have few doubts to clear before going deep to my application..

please give brief answer to my question...

  1. is it possible to have 2 spinners in one view?
  2. can i add setOnItemSelectedListener(this) for both?
  3. if yes, how could i know which option is selected on both the spinners? like spinner.getSelectedItemPosition(); on both the spinners?

waiting for ur valuable replies. reference materials are also appreciable

like image 422
Dil Se... Avatar asked Dec 10 '22 04:12

Dil Se...


2 Answers

Yes, you can have multiple spinner's that can be managed by single listener or you can have seperate Listener for each.

For Single Listener you can do something like this,

Set the Listener

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

Then you can use onItemSelected to get perform the action related to the spinner's.

    @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                                                      int position,long arg3) 
        {
        int id = parent.getId();
        switch (id) 
        {
                case R.id.spinner1:
                // your stuff here
                break;
            case R.id.spinner2:
               // your stuff here
            break;
        }
like image 65
Lalit Poptani Avatar answered Dec 21 '22 10:12

Lalit Poptani


Check this:

Spinner spinner0, spinner1, spinner2; TextView textView0, textView1, textView2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView0 = (TextView) findViewById(R.id.text0);
    spinner0 = (Spinner) findViewById(R.id.spinner0);

    ArrayAdapter<String> adapter0 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, text0);
    adapter0.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner0.setAdapter(adapter0);
    spinner0.setOnItemSelectedListener(onItemSelectedListener0);

    textView1 = (TextView) findViewById(R.id.text1);
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, text1);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(adapter1);
    spinner1.setSelection(0,false); //make sure only one listener is 

//active in the onCreate method by setting the other listener as false spinner1.setOnItemSelectedListener(onItemSelectedListener1);

    textView2 = (TextView) findViewById(R.id.text2);
    spinner2 = (Spinner) findViewById(R.id.spinner2);
    MySpinnerAdapter adapter2 = new MySpinnerAdapter(MainActivity.this, android.R.layout.simple_spinner_item, obj2);
    //adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(adapter2);
   spinner2.setSelection(0,false);
    spinner2.setOnItemSelectedListener(onItemSelectedListener2);

}

OnItemSelectedListener onItemSelectedListener0 = new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
      //do your stuff spinner0
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
    }
};

OnItemSelectedListener onItemSelectedListener1 = new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        //do your stuff spinner1
    }

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

};

OnItemSelectedListener onItemSelectedListener2 = new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        //do your stuff spinner2
    }

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

};

}

like image 45
UYI EKE Avatar answered Dec 21 '22 11:12

UYI EKE