简体   繁体   中英

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>

The reason all your checkboxes are getting checked is that they all have the same id . If either one of them is checked and they are recreated(either because of orientation change or reattaching a fragment), Android system tries to restore their state and it identifies them based on their ids. Hence all of them get checked.

android:saveEnabled flag tells Android system whether to save their state and try to restore later or not.

Since you already have a mechanism in place to restore their state setting android:saveEnabled to false works for you.

See this question where a similar thing happens with EditText : Why does Android change the value of EditTexts with same id?

checkedListSet.get(item) != null

This method will return true if there is an item in your list and it's not null.

You are assigning the result of this expression to your isChecked boolean.

Thus every time this expression returns true, your check box will be setChecked true.

I do not know what exact condition you want to check before setting the check box so i can't help you with that.

still not sure why, but after put in android:saveEnabled="false" the same code start to work. does anyone know why it has to have android:saveEnabled="false"?

<CheckBox
        android:saveEnabled="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM