1

我有下面的代码,问题是所有膨胀的视图都具有相同的 id(在 中设置player_item.xml) - 所以当我旋转屏幕时,它们都恢复为相同的值。最简单的解决方案是删除这些 id 并让 android 自动生成它们,但我不能这样做,因为我需要它们在相对布局中定位。任何想法我该如何解决这个问题?

player_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/player_name"
        style="@style/stats_edit_column"
        android:layout_width="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/goals"
        android:gravity="center_vertical" />

    <EditText
        android:id="@+id/goals"
        style="@style/stats_edit_column"
        android:layout_toLeftOf="@+id/assists"
        android:inputType="number"
        android:textSize="@dimen/text_size_normal" />

    <EditText
        android:id="@+id/assists"
        style="@style/stats_edit_column"
        android:layout_alignParentRight="true"
        android:inputType="number"
        android:textSize="@dimen/text_size_normal" />

</RelativeLayout>

代码

for (PlayerStats playerStats : allPlayers) {
    View playerStatsView = getLayoutInflater().inflate(R.layout.player_item, null);

    TextView playerName = (TextView) playerStatsView.findViewById(R.id.player_name);
    playerName.setText(playerStats.getPlayerName());

    EditText goalsEditText = (EditText) playerStatsView.findViewById(R.id.goals);
    goalsEditText.setText(String.valueOf(playerStats.getGoals()));

    EditText assistsEditText = (EditText) playerStatsView.findViewById(R.id.assists);
    assistsEditText.setText(String.valueOf(playerStats.getAssists()));

    editLayout.addView(playerStatsView);
}
4

2 回答 2

0

我建议为每个表单创建一个 bean,并在移动到其他活动时将所有表单值设置为 bean,并将 bean 引用存储到共享首选项中,所以当你回到同一个活动时,只需重新加载从bean ,这有助于保存表单的状态

我希望它有帮助

于 2012-11-14T13:21:47.647 回答
0

在标签中保留对 playerStats 的引用怎么样?

for (PlayerStats playerStats : allPlayers) {
    View playerStatsView = getLayoutInflater().inflate(R.layout.player_item, null);

    TextView playerName = (TextView) playerStatsView.findViewById(R.id.player_name);
    playerName.setText(playerStats.getPlayerName());

    EditText goalsEditText = (EditText) playerStatsView.findViewById(R.id.goals);
    goalsEditText.setText(String.valueOf(playerStats.getGoals()));

    EditText assistsEditText = (EditText) playerStatsView.findViewById(R.id.assists);
    assistsEditText.setText(String.valueOf(playerStats.getAssists()));

    playerStatsView.setTag(playerStats);

    editLayout.addView(playerStatsView);

}

getViewForPlayerStatsByTag(PlayerStats playerStats){
    for (int i=0;i<editLayout.getChildCount();i++){
        view = (View)editLayout.getChildAt(i);
        if (view.getTag()==playerStats){return view;}
    }
    return null;
}

getViewForPlayerStatsByTag 将返回这些统计信息的 player_item 视图组,如果不存在则返回 null。

于 2012-11-14T14:30:22.990 回答