4

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.

2
  • I have tried to follow tutorials as mkyong.com/android/android-spinner-drop-down-list-example but there aren't enough info to allow me to configure two spinner in the same activity as described Commented Dec 5, 2012 at 4:28
  • from my point of view, i can't even conceive the problem. if you know how to do what you want for one spinner, why can't you do it for the other? posting your java code and explaining what you want to happen but isn't happening would help very much. Commented Dec 5, 2012 at 4:31

3 Answers 3

9

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; } } 
Sign up to request clarification or add additional context in comments.

Comments

2

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 

1 Comment

Thanks for the answer. Could you clarify me how to set the selected item to String selectedAge, and String selectedSex using this code?
0

Try This method

spinner1 = (Spinner) findViewById(R.id.spinner); spinner2 = (Spinner) findViewById(R.id.highschoolspinner); List<String> categories = new ArrayList<String>(); categories.add("Qualification"); categories.add("High School"); categories.add("Higher Secondary/PUC"); categories.add("Diploma"); categories.add("Degree"); categories.add("Master Degree"); List<String> list = new ArrayList<String>(); list.add("Plus One"); list.add("Plus Two"); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinnertext, categories); adapter.setDropDownViewResource(android.R.layout.select_dialog_item); spinner.setAdapter(adapter); ArrayAdapter<String> dataAdapter12 = new ArrayAdapter<String>(this, R.layout.spinnertext, list); dataAdapter12.setDropDownViewResource(android.R.layout. simple_spinner_dropdown_item); dataAdapter12.notifyDataSetChanged(); highschool.setAdapter(dataAdapter12) 

Am used custom adapter to load data you can use built in adapter like

 ArrayAdapter dataAdapter11 = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.