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

  • Home
  • SEARCH
  • 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 8386361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:54:55+00:00 2026-06-09T17:54:55+00:00

in the program I inflate several Spinners and add them to a layout. For

  • 0

in the program I inflate several Spinners and add them to a layout. For each spinner I make an individual selection. This works fine if the Activity is created as usual – so every spinner gets his own selection. If the activity is created due to a rotation it will apply the value of the last spinner to all spinner and I watched that a onItemSelectedListener will be called multiple times. I am very desperate since that seems not very logic to me. Here is the code:

SpinnerTesterActivity.xml

public class SpinnerTesterActivity extends Activity {
    public String[] array={"s0","s1","s2","s3","s4","s5","s6","s7","s8","s9"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Log.d("SPINNERTESTER", "REPAINT ACTIVITY");
        LinearLayout layout=(LinearLayout)this.findViewById(R.id.parentLayout);

        for (int i = 0 ; i<3; i++){
            LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            ViewGroup reminderLayout = (ViewGroup) layoutInflater.inflate(R.layout.edit_item, null);
            Spinner spinner = (Spinner) reminderLayout.findViewById(R.id.spinner);
            spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array));
            spinner.setSelection(i);

            layout.addView(reminderLayout);

        }
    }

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/parentLayout" >
</LinearLayout>

edit_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingBottom="3dip"
    android:paddingTop="3dip" >

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" />

</LinearLayout>
  • 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-09T17:54:56+00:00Added an answer on June 9, 2026 at 5:54 pm

    I believe the default save and restore functions identify each View by its id, since all of your Spinners have the same id they receive the same data. Simply give the Spinners their own ids, even a generic id like this:

    spinner.setId(i);
    

    A couple other quick points:

    1. Your layout edit_item.xml has a LinearLayout with only one child, this is unnecessary. Simply use the Spinner as the root element:

      <Spinner
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/spinner"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" />
      
    2. You are fetching the LayoutInflater on each loop, you only need to do this once.

    All together:

        LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout layout=(LinearLayout)this.findViewById(R.id.parentLayout);
    
        for (int i = 0 ; i<3; i++){
            Spinner spinner = (Spinner) layoutInflater.inflate(R.layout.spinner, null);
            spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array));
            spinner.setId(i);
            spinner.setSelection(i);
    
            layout.addView(spinner);
        }
    

    Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This program runs just fine, but for some reason when I ask for an
In ViewPagerAdapter.class I have 2 layout(whereami.xml and mapview.xml), and use LayoutInflater to inflate each
My program switches between two Activities that each inflate a derived GLSurfaceView that uses
Program that calculates and shows the value of (2 to the 10th power) This
This program builds a dictionary out of a list based on what two index
This program returns: package main import ( flag fmt ) func main() { num_agents
My program processes PDF files and reads some streams out of them. There are
I am studying to fragmentation in android, I write this simple program: In this
I am trying to learn more about java. This program is an attempt to
Program received signal: 0. warning: check_safe_call: could not restore current frame What is this

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.