【发布时间】:2017-02-16 17:29:33
【问题描述】:
具有几个动态充气/添加的复选框,当有些是检查旋转或最小化应用程序(为了易于触发这种情况,打开'不保留活动活动'),还原的UI视图显示了检查的所有复选框。
当操作系统执行 saveInstance 时,我们将选中的项目存储在一个列表中,当操作系统恢复片段时,我们得到选中项目的列表,当重新填充复选框行时,它调用 setChecked(true) 或 setChecked(false ) 基于列表。 但是在那之后所有的复选框都显示为选中,虽然在调试中它清楚地显示只有选中的那些被使用'true'而其他的被使用'false'和setChecked()。
任何人都经历过同样的事情,或者知道为什么单个 checkBox 实例上的 setChecked() 不执行所谓的操作吗?
itemList = [A, B, C, D, E]
checkedListSet = [A, B]
void insertOneRow(ViewGroup container, Item item) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemLayout = inflater.inflate(R.layout.item_row, null, false);
TextView txt = (TextView)itemLayout.findViewById(R.id.text);
txt.setText(item.toString());
container.addView(itemLayout, container.getChildCount());
CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.checkbox);
if (checkbox != null) {
boolean isChecked = (checkedListSet.get(item) != null);
Log.i(“insertOneRow(), isChecked:"+ isChecked +", item:”+item);
checkbox.setChecked(isChecked); //<== trace shows only A and B are set with true
}
}
item_row.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="36dp"
android:orientation="horizontal"
>
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:checked="false" />
<TextView
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
【问题讨论】:
-
checkedListSet对象的类型是什么?我认为列表上的 get 方法只接受一个 int 作为索引。 -
checkedList 只是一个包含被检查项的列表,可以是 ListArray
- 或 HashSet
- ,checkedListSet.get(item) != null 只是想说该项目是否存在在 checkList 中,checkBox 需要设置为 true。
- 或 HashSet
标签: android checkbox onsaveinstancestate