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

android, android-layout, android-spinner

Solution

Try this

 ArrayAdapter<CharSequence> adapterAge; 
 ArrayAdapter<CharSequence> adapterSex;

 String[] AgeArr = {"18-20", "19-21"};
 String[] sexArr = {"male", "female"};

 Spinner ageDrp =(Spinner)findViewById(R.id.spAge);
 Spinner sex1Drp    =(Spinner)findViewById(R.id.spSex);

adapterAge =    new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item,AgeArr);
adapterAge.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ageDrp.setAdapter(adapterAge);

adapterSex=     new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item,sexArr);
adapterSex.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sexDrp.setAdapter(adapterSex);

String selectedAge  = ageDrp.getSelectedItem().toString();
String selectedSex  = sexDrp.getSelectedItem().toString();
System.out.println(selectedAge+" "+selectedSex);// check the output in logcat

Problem

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.

Original source