I am adding say ‘n’ spinners in a loop. But only last added spinner is visible when I ran my code. Please see my code below
Context context = this;
ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
ArrayList<String> hi = new ArrayList<String>();
hi.add("Test 1");
hi.add("Test 2");
list.add(hi);
ArrayList<String> hi1 = new ArrayList<String>();
hi1.add("Test 3");
hi1.add("Test 4");
list.add(hi1);
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Test App");
alert.setMessage("Testing");
for (int i = 0; i < 2; i++) {
final Spinner spinner = (Spinner) layout
.findViewById(R.id.spinner1);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list.get(i));
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
alert.setView(spinner);
}
alert.show();
That is happening because all you do in your
forloop is searching for aSpinnerin theActivity‘ layout and then using thesetViewmethod of theAlertDialogto add it(this method doesn’t add the views to the otherViewsalready present, it just replaces them). If you want to add multipleSpinnerto the sameAlertDialogyou should do it like this:Edit :
You can add more views by using a proper layout manager. For example, for a
TextViewand aSpinneron the same row you could use aTableLayout: