1

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>
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Anthea
  • 3,741
  • 5
  • 40
  • 64

1 Answers1

1

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!

Sam
  • 86,580
  • 20
  • 181
  • 179
  • thanks that worked. Also thanks for the other advises. I just developed that code to break the problem down to the main idea. – Anthea Aug 14 '12 at 18:18