有幾個動態充氣/添加複選框,當一些被選中時旋轉或最小化應用程序(爲了便於觸發該情況,打開'不保持活動活着'),恢復的UI視圖顯示所有選中的複選框。爲什麼checkBox是在setChecked(false)被調用時檢查的
當os執行saveInstance時,我們將檢查的項目存儲在一個列表中,當操作系統恢復片段時,我們得到檢查項目的列表,並且當重新填充複選框行時,它會調用setChecked(true)或setChecked假)基於列表。 但之後,所有的複選框顯示爲選中狀態,雖然在調試中它清楚地顯示只有選中的選項被使用'true',而其他選項使用setChecked()選項'false'。
任何人都會遇到同樣的情況,或者知道爲什麼單個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'對象?我認爲在列表上獲取方法只接受一個int作爲索引。 –
checkedList只是一個包含檢查項的列表,可能是ListArray- 或HashSet
- ,checkedListSet.get(item)!= null只是想說如果該項存在於checkList中,那麼checkBox需要設置爲true。 –
lannyf