简体   繁体   English

Android使多个微调框膨胀会导致奇怪的选择行为

[英]Android inflating multiple spinner causes strange selection behavior

in the program I inflate several Spinners and add them to a layout. 在程序中,我给几个Spinners充气并添加到布局中。 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. 如果活动是由于旋转而创建的,则它将最后一个微调器的值应用于所有微调器,并且我观察到onItemSelectedListener将被多次调用。 I am very desperate since that seems not very logic to me. 我非常绝望,因为这对我来说似乎不是很合逻辑。 Here is the code: 这是代码:

SpinnerTesterActivity.xml 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 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 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>

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. 我相信默认的保存和还原功能会通过其ID识别每个视图,因为所有微调器都具有相同的ID,它们将接收相同的数据。 Simply give the Spinners their own ids, even a generic id like this: 只需为Spinners提供他们自己的ID,甚至是一个通用ID,如下所示:

spinner.setId(i);

A couple other quick points: 其他几个要点:

  1. Your layout edit_item.xml has a LinearLayout with only one child, this is unnecessary. 您的布局edit_item.xml具有一个只有一个孩子的LinearLayout,这是不必要的。 Simply use the Spinner as the root element: 只需使用Spinner作为根元素:

     <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. 您在每个循环上都提取LayoutInflater,只需要执行一次即可。

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! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM