在程序中,我给几个 Spinner 充气并将它们添加到布局中。对于每个微调器,我都会进行单独的选择。如果 Activity 像往常一样创建,这很好用——所以每个微调器都有自己的选择。如果活动是由于旋转而创建的,它会将最后一个微调器的值应用于所有微调器,我看到一个 onItemSelectedListener 将被多次调用。我非常绝望,因为这对我来说似乎不太合乎逻辑。这是代码:
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);
}
}
主要的.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>
编辑项目.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>