Android 旋转视图会使动态添加的复选框具有相同的文本

Android 旋转视图会使动态添加的复选框具有相同的文本,android,android-layout,Android,Android Layout,我使用LayoutInflater动态生成多个TableRows,每个TableRows包含一个复选框。在你旋转显示器之前,一切都很顺利。每个复选框获得与最后创建的复选框相同的文本和选中状态 我发现,如果我为每一行和每一个复选框分配了一个唯一的ID,它就会正常工作(每个复选框都保持其唯一的文本和选中状态) 如果多次膨胀同一布局,是否需要为膨胀布局内的每个视图指定唯一ID?如果你有一个正在膨胀的复杂布局,那肯定会很痛苦 以下是我的代码: 主要活动 public class PrototypeAct

我使用LayoutInflater动态生成多个TableRows,每个TableRows包含一个复选框。在你旋转显示器之前,一切都很顺利。每个复选框获得与最后创建的复选框相同的文本和选中状态

我发现,如果我为每一行和每一个复选框分配了一个唯一的ID,它就会正常工作(每个复选框都保持其唯一的文本和选中状态)

如果多次膨胀同一布局,是否需要为膨胀布局内的每个视图指定唯一ID?如果你有一个正在膨胀的复杂布局,那肯定会很痛苦

以下是我的代码:

主要活动

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

        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        TableLayout tablesLayout = (TableLayout)findViewById(R.id.TableLayout1);
        tablesLayout.removeAllViews();

        String[] values = new String[] { "Item 1", "Item 2", "Item 3"};     

        for (int i = 0; i < values.length; i++) {
            View view = inflater.inflate(R.layout.row_layout, null, false);
            view.setId(i);
            CheckBox checkBox = (CheckBox) view.findViewById(R.id.tableCollectCheckBox);
            checkBox.setId(values.length+i);
            checkBox.setText(values[i]);
            tablesLayout.addView(view);
        }
        tablesLayout.invalidate();
    }
}
公共类原型活动扩展活动{
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutFlater充气器=(LayoutFlater)getSystemService(Context.LAYOUT\u充气器\u服务);
TableLayout TableLayout=(TableLayout)findViewById(R.id.TableLayout1);
tableLayout.removeAllViews();
字符串[]值=新字符串[]{“项目1”、“项目2”、“项目3”};
对于(int i=0;i
main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/LinearLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TableLayout
                android:id="@+id/TableLayout1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"/>

        </LinearLayout>
    </ScrollView>
</FrameLayout>

row_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<TableRow 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:orientation="horizontal">
        <CheckBox
            android:id="@+id/tableCollectCheckBox"
            android:text="DataTable"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</TableRow>


你应该打电话。

我试过设置冻结文本。这使得我在重新创建复选框时不必重新分配文本,但似乎我仍然必须为每个复选框分配唯一的ID,否则每个复选框都会获得上次创建的复选框的文本和选中状态。我真正的问题是“我是否需要为多次膨胀的膨胀布局内的每个视图分配唯一的ID?”您必须确保复选框始终具有相同的ID,即使在配置更改期间也是如此。所有状态都已保存并与视图的id关联。如果id未持久化,则在更改配置后,状态将丢失或不正确(甚至损坏)。明白了。每个视图都需要分配一个唯一的ID,否则操作系统会错误地使用可能属于另一个视图的持久化状态更新每个视图。