0

I have a RadioGroup with 3 radiobuttons, say A, B and C, when I click on A, it will pop up a dialog, and using setSingleChoiceItems of the android dialog, it presents another 4 choices. The problem is that, if I clicked cancel on this dialog, A is now still being checked. How can I make sure that if the user has pressed cancel, A is remained unchecked? I tried the following:

 CustomizedDialogFragment testDiag = new CustomizedDialogFragment();
           testDiag.show(getFragmentManager(), "Diag");
           if (testDiag.userChecked()){
               radioGroup.check(0);
           } else {
               radioGroup.clearCheck();
           }

    public static class CustomizedDialogFragment extends DialogFragment {
    private boolean checked = false;
    @Override
    public Dialog onCreateDialog(final Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setSingleChoiceItems(R.array.test_values, -1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case 0: break;
                            case 1: break;
                            case 2: break;
                            case 3: break;
                        }
                    }
                })

                .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        checked = true;
                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        checked = false;
                    }
                        });
        return builder.create();
    }

    public boolean userChecked(){
        return checked;
    }

}

However, this does not work. Would really appreciate for anytype of help.

loneranger
  • 53
  • 2
  • 6

1 Answers1

0

EDIT: added static method to create instance of a fragment

private RadioGroup mGroup;

public static CustomizedDialogFragment create(RadioGroup group){
    CustomizedDialogFragment instance = new CustomizedDialogFragment(group);
    return instance;
}

protected CustomizedDialogFragment (RadioGroup group){
    mGroup = group;
}

Change negative button click to:

setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        //Clear RadioGroup selection
                        mGroup.check(-1);
                    }
                        });
Anton Kovalyov
  • 932
  • 4
  • 13
  • Hey, thanks! it worked, but I am pretty sure I read somewhere that it is not recommended to pass an argument to the constructor of a fragment? Thats why I am confused with how to go on about solving this problem. Am i missing something here? – loneranger Jun 07 '15 at 16:39
  • @loneranger You are right. I edited my answer. But not completely sure about it. – Anton Kovalyov Jun 07 '15 at 16:42
  • thanks for all the help! android studio auto suggestion is using the bundle, which Im not sure how to manuplate the bundle and pass on the radioGroup. – loneranger Jun 07 '15 at 17:11
  • i have this problem where, when i use mGroup.check(-1), the dialog will somehow pop up twice – loneranger Jun 12 '15 at 17:44