jeudi 16 février 2017

why the checkBox is check when setChecked(false) is called

having a few dynamically inflated/added checkbox, when some are checked the rotate or minimize the app (for easy to trigger the case, turn on the 'Dont keep activity alive'), the restored UI view shows all checkbox checked.

When os do saveInstance we store the checked items in a list, and when the OS to restore the fragment, we get the list of checked items and when re inflate the checkbox row it calls either setChecked(true) or setChecked(false) based on the list. But after that all the checkbox shows as checked, although in the debug it clearly shows only the checked ones are used 'true' and others are used 'false' with setChecked().

Anyone experienced the same, or knows why setChecked() on the individual checkBox instance does not do what is called?

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>




Aucun commentaire:

Enregistrer un commentaire