如何在 DialogFragment 中显示您复出时检查的项目

How to show the item that you check when comeback in a DialogFragment

我有一个显示星期几的 DialogFragment 和一个复选框。我的问题很简单(但我做不到)如何在再次打开 DialogFragment 时将我的选择保存到我可以看到上次启用的日期。

因为我不能做这样的事情 mSelectedItems.add(which).setChecked(true).

提前致谢。

public class DayFragment extends DialogFragment  {

    protected String[] listitems = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
       final ArrayList mSelectedItems = new ArrayList();  // Where we track the selected items
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        builder.setMultiChoiceItems(listitems, null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        if (isChecked) {
                            // If the user checked the item, add it to the selected items
                            mSelectedItems.add(which);

                            if(listitems[which].equals("Monday")){
                                Toast.makeText(getActivity(), listitems[which], Toast.LENGTH_SHORT).show();
                            }


                        } else if (mSelectedItems.contains(which)) {
                            // Else, if the item is already in the array, remove it
                            mSelectedItems.remove(Integer.valueOf(which));
                            if(listitems[which].equals("Monday")){
                                Toast.makeText(getActivity(), "Unchecked " + listitems[which], Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                })
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {

                    }
                });

        return builder.create();
    }

您正在设置为空检查值数组: builder.setMultiChoiceItems(listitems, null, new DialogInterface.OnMultiChoiceClickListener());

勾选this。您需要做的是将选中的值保存为 SharedPreferences 中的一个集合,然后在执行 setMultiChoiceItems 之前从 SharedPreference.

中取回这些值