ソリューションのトリッキーな部分はView
オブジェクトを取得することでした。 View
を取得すると、チェックボックスのUIを更新するためにinvalidate()
とすることができます。ここで
は私の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?
}
}