Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Implements multiple spinner with different item list and different action on click in the same Activity

I want to implement two different spinner in Android, the spinner have different data set

This is the spinner with the age, that uses a defined String array with all age ranges (es 18-20, 19-21 etc.)

 <Spinner
        android:id="@+id/spAge"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:entries="@array/age_array" 
        tools:listitem="@android:layout/simple_spinner_item/>

And this is the spinner with the sex, that show only the two items Male and Female

 <Spinner
       android:id="@+id/spSex"
       android:layout_width="match_parent"
       android:layout_height="35dp"
       android:entries="@array/sex_array"
       tools:listitem="@android:layout/simple_spinner_item />

For each selected item the my activity should set the associated selected items values to the two Objects:

String selectedAge;
String selectedItem;

The sample that I have seen doesn't contains multiple spinner with different items set and different actions on item selected, and I don't know how to solve the problem.

like image 570
AndreaF Avatar asked Dec 20 '22 12:12

AndreaF


1 Answers

write your Code as below to do different actions on item selected.

    spinner1.setOnItemSelectedListener(this);
    spinner2.setOnItemSelectedListener(this);
  public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub
    switch(arg0.getId()){
             case R.id.spAge :
                   //Your Action Here.
                   break;
             case R.id.spSex :
                   //Your Another Action Here.
             break;
            }
}
like image 105
Nirav Tukadiya Avatar answered Dec 27 '22 03:12

Nirav Tukadiya