Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7899259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:39:13+00:00 2026-06-03T08:39:13+00:00

I am adding say ‘n’ spinners in a loop. But only last added spinner

  • 0

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();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:39:15+00:00Added an answer on June 3, 2026 at 8:39 am

    That is happening because all you do in your for loop is searching for a Spinner in the Activity‘ layout and then using the setView method of the AlertDialog to add it(this method doesn’t add the views to the other Views already present, it just replaces them). If you want to add multiple Spinner to the same AlertDialog you should do it like this:

    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    for (int i = 0; i < 2; i++) {
         Spinner spinner = new Spinner(this);
         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);
         LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
         ll.addView(spinner, params);
    }
    alert.setView(ll); 
    

    Edit :
    You can add more views by using a proper layout manager. For example, for a TextView and a Spinner on the same row you could use a TableLayout:

        TableLayout tl = new TableLayout(this);
        for (int i = 0; i < 2; i++) {
            TableRow tr = new TableRow(this);
            TextView t = new TextView(this);
            t.setText("Text " + i);
            TableRow.LayoutParams params1 = new TableRow.LayoutParams(
                    TableRow.LayoutParams.WRAP_CONTENT,
                    TableRow.LayoutParams.WRAP_CONTENT);
            tr.addView(t, params1);
            Spinner spinner = new Spinner(this);
            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);
            TableRow.LayoutParams params2 = new TableRow.LayoutParams(
                    TableRow.LayoutParams.FILL_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT);
            tr.addView(spinner, params2);
            TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.FILL_PARENT,
                    TableLayout.LayoutParams.WRAP_CONTENT);
            tl.addView(tr, trParams);
        }
        alert.setView(tl);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say i've got a simple Map which is queried frequently, but only very
Let's say I am adding li elements in javascript code with jQuery. Example 1:
Let's say customer is adding multiple products to cart. Add car, apartment, tour, some
Say, you are submitting a form, which affects your database (adding records/ deleting them/
Adding Exceptional Handling is a good practise, but I have a doubt, In our
Adding the row one by one in the button click.. (i tried but its
Adding strings localization files in iPhone bundle is very simple and perfectly fine but
When adding code to an onclick handler, are there any limitations to what can
Say you are adding x number of objects to a collection, and after or
Let's say I have a form for adding/editing products (with field 'user' being a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.