持久多选列表 - ArrayList重置

时间:2017-12-29 15:48:03

标签: android fragment dialogfragment

Android文档为多选DialogFragment提供以下代码,并将其描述为“持久性多选列表”

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    mSelectedItems = new ArrayList();  // Where we track the selected items
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Set the dialog title
    builder.setTitle(R.string.pick_toppings)
            // Specify the list array, the items to be selected by default (null for none),
            // and the listener through which to receive callbacks when items are selected
            .setMultiChoiceItems(R.array.toppings, 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);
                            } else if (mSelectedItems.contains(which)) {
                                // Else, if the item is already in the array, remove it
                                mSelectedItems.remove(Integer.valueOf(which));
                            }
                        }
                    })
            // Set the action buttons
            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    // User clicked OK, so save the mSelectedItems results somewhere
                    // or return them to the component that opened the dialog
               ...
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
               ...
                }
            });

    return builder.create();
}

https://developer.android.com/guide/topics/ui/dialogs.html

所选项目保存在ArrayList中,并且所选项目在方向更改时保持选择状态是持久的,但是ArrayList被重置,如果我通过接口将其发送回我的活动,我发送一个新的空ArrayList

我是否必须覆盖onSaveInstanceState或我错过了什么?

0 个答案:

没有答案