Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android DialogFragment-多选对话框复选框不更新/需要两次单击_Android_Android Dialogfragment_Android Dialog - Fatal编程技术网

Android DialogFragment-多选对话框复选框不更新/需要两次单击

Android DialogFragment-多选对话框复选框不更新/需要两次单击,android,android-dialogfragment,android-dialog,Android,Android Dialogfragment,Android Dialog,我认为这只在最近的Android API中才开始发生,但问题是,当您第一次尝试在多选择对话框中选择复选框时,需要额外的点击才能更新UI 我很确定这是由于Android的错误,因为我的代码非常简单 经过大量的实验,我找到了答案,因此将在下面分享…解决方案的棘手部分是获取视图对象。一旦有了视图,就可以使其失效()以更新复选框的UI 下面是我的DialogFragment子类的要点: public class MyMultiChoiceDialogFragment extends DialogFrag

我认为这只在最近的Android API中才开始发生,但问题是,当您第一次尝试在多选择对话框中选择复选框时,需要额外的点击才能更新UI

我很确定这是由于Android的错误,因为我的代码非常简单


经过大量的实验,我找到了答案,因此将在下面分享…

解决方案的棘手部分是获取
视图
对象。一旦有了
视图
,就可以
使其失效()
以更新复选框的UI

下面是我的
DialogFragment
子类的要点:

public class MyMultiChoiceDialogFragment extends DialogFragment {

    private View mView = null;

    @Override @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(title);
        builder.setMultiChoiceItems(
            cursor,
            isCheckedColumn
            labelColumn
            new DialogInterface.OnMultiChoiceClickListener() {

            @Override
            public void onClick(DialogInterface dialogInterface, int which, boolean isChecked) {

                // Handle the checkbox de/selection

                /*
                 * The problem is that, despite onClick being called (with the correct parameter values), the
                 * checkbox ticks were not updating on the UI.
                 * Solution is to invalidate/redraw the layout so the checkboxes also update visually
                 */
                mView.invalidate();
                mView.forceLayout(); // Following tests, this line is also required.

            }
        });

        AlertDialog dialog = builder.create();

        /*
         * This seems to be the only way to get the view.
         * Save it in an instance variable so we can access it within onClick()
         */
        mView = dialog.getListView();

        return dialog;
    }    

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mView = null; // Clean up / prevent memory leak - necessary?
    }

}