Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在方向更改(屏幕旋转)时,EditText都显示相同的值,因为它们共享相同的id_Android - Fatal编程技术网

Android 在方向更改(屏幕旋转)时,EditText都显示相同的值,因为它们共享相同的id

Android 在方向更改(屏幕旋转)时,EditText都显示相同的值,因为它们共享相同的id,android,Android,我有下面的XML定义,我通过编程多次实例化它,具体取决于数据库中的项目数。每个实例都是一个TableRow,稍后添加到TableLayout。TableLayout在其他地方定义(在活动XML定义中) group_editbinds.xml <?xml version="1.0" encoding="utf-8"?> <TableRow xmlns:android="http://schemas.android.com/apk/res/android" android:id

我有下面的XML定义,我通过编程多次实例化它,具体取决于数据库中的项目数。每个实例都是一个
TableRow
,稍后添加到
TableLayout
TableLayout
在其他地方定义(在活动XML定义中)

group_editbinds.xml

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

    <TextView
      android:id="@+id/textView1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="Round" />

    <EditText
      android:id="@+id/editText2"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:ems="2"
      android:hint="Small"
      android:inputType="number"
      android:layout_weight="4"
      android:layout_marginRight="10dip"
      android:text="0" />

    <EditText
      android:id="@+id/name"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:ems="2"
      android:hint="Big"
      android:inputType="number"
      android:layout_weight="4"
      android:layout_marginRight="10dip"
      android:paddingRight="6dip"
      android:text="0" >
    </EditText>

    <CheckBox
      android:id="@+id/checkBox1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1" />

</TableRow>
当我进入“活动”编辑日程安排时,这些值如下所示:

1 2
2 4 
4 8
16 32
100 200
当我旋转屏幕时,我得到:

100 200
100 200
100 200
100 200
100 200

同样,我认为这是因为我的所有视图都具有相同的Id,因为相同的XML资源被多次实例化。解决这种烦恼的正确方法是什么?

我同意Vilen Melkumyan的观点,即您应该使用
列表视图
ListView
Adapter
起初可能很吓人,但当您学会使用它们时,它们将被证明是强大的工具

要回答您的问题,如果您想继续使用
表格布局
,可以将以下内容添加到
AndroidManifest.xml
文件中的活动中:

            android:configChanges="keyboardHidden|orientation|screenSize" >

这将防止在方向状态更改后重新加载
活动。

如果同一视图项被多次实例化,为什么不使用ListView?好吧,这可能是一个很好的观点。。。也许是解决方案(如果我将代码移植到ListView)。我选择不使用ListView可能是因为我不太了解它的用途。是的,我注意到:)事实上,只有当您的数据是恒定的,并且您知道从Bing开始有多少ROE时,才应该使用TableLayout,否则,在您的情况下,您应该将ListView与自定义项和适配器一起使用。我将我的代码转换为一个
ListView
,并得出结论,这是“错误的做法”。在
列表视图中包含
EditText
字段是一件非常糟糕的事情。没有完美的解决方案可以使
EditText
聚焦。您可以在StackOverflow中搜索“EditText ListView focus”,并查看无数问题和潜在解决方案——每个解决方案在某些情况下都有某些缺点。我想我需要回到我最初的实现,我祈祷TmKVU的答案对我有用。此外,嵌套的
EditText
s如果属于
ListView
,在方向更改时不会保留其状态。一般来说,Android似乎无法正确引用这些视图,应该避免使用。@mindandsky这是可能的。使用自定义适配器(扩展BaseAdapter类),可以为列表行使用任何类型的视图,包括嵌套视图。如果你看到我的其他评论,你会发现我在
ListView
中列出了嵌入
EditText
的一些问题。也许你该小心点,这样你就不会走我走过的路了!:为我工作!但是人们推荐碎片。与碎片相比,这种方法有什么缺点吗?或者这可以被认为是一种标准的方式?@mangeshhotage这个问题和答案与碎片无关。使用
适配器的
列表视图
方法仍然是处理列表的最佳方法。我建议你读一下这本书中的片段
            android:configChanges="keyboardHidden|orientation|screenSize" >