如何将arrayList传递给AlertDialog片段

时间:2016-12-29 16:30:01

标签: android alertdialog

所以我有AlertDialog呈现MultiChoiceItemsList,如下所示,

public class CustomDayRepeatFragment extends DialogFragment {

    public interface DayRepeatListner {
        void onFinishDayRepeatListner(List<String> days);
    }
    //List to save the result
    private ArrayList<String> selectedDays;
    String weekdays[];
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        selectedDays = new ArrayList<>();
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        weekdays = getResources().getStringArray(R.array.weekdays);
        Log.d("In dialog", "Custom");
        builder.setTitle("Select Days to Repeat")
                .setMultiChoiceItems(weekdays, null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        if(isChecked) {
                            selectedDays.add(weekdays[which]);
                        } else if (selectedDays.contains(weekdays[which])) {
                            selectedDays.remove(String.valueOf(weekdays[which]));
                        }
                    }
                })
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        for(int i = 0; i < selectedDays.size(); i++) {
                            Log.d("Selected Days", selectedDays.get(i));
                        }

                    }
                });

        return builder.create();
    }
}

我从我的主要活动中调用它如下

 CustomDayRepeatFragment dialog = new CustomDayRepeatFragment();
 dialog.show(getSupportFragmentManager(), DIALOG_REPEAT_SETTING);

但是,我想向此List<string>发送AlertDialog,然后使用此列表。

我无法弄清楚如何实现它。

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您可以在CustomDayRepeatFragment课程中创建静态newInstance方法,然后按照以下方式从MainActivity调用它。在这里,您可以将列表作为参数发送

CustomDayRepeatFragment dialog = CustomDayRepeatFragment.newInstance(list);
 dialog.show(getSupportFragmentManager(), DIALOG_REPEAT_SETTING);

现在在newInstance() method类的CustomDayRepeatFragment中,您可以将列表作为Bundle中的参数保存为bundle.putParcelableArrayList("list", list);,然后从列中的参数中检索它。 onCreateDialog() methodList<String> list = getArguments().getParcelableArrayList("list");

你的newInstance和onCreateDialog方法看起来像

@NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        selectedDays = new ArrayList<>();


        List<String> list = (List<String>) getArguments().getStringArrayList("list");  // the list you pass as an argument

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        weekdays = getResources().getStringArray(R.array.weekdays);
        Log.d("In dialog", "Custom");
        builder.setTitle("Select Days to Repeat")
                .setMultiChoiceItems(weekdays, null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        if(isChecked) {
                            selectedDays.add(weekdays[which]);
                        } else if (selectedDays.contains(weekdays[which])) {
                            selectedDays.remove(String.valueOf(weekdays[which]));
                        }
                    }
                })
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {


                        for(int i = 0; i < selectedDays.size(); i++) {
                            Log.d("Selected Days", selectedDays.get(i));
                        }

                    }
                });

        return builder.create();
    }

    //Creating a fragment instance class to pass the parameters
    public static CustomDayRepeatFragment newInstance(int groupPosition) {
        CustomDayRepeatFragment fragment = new CustomDayRepeatFragment();

        Bundle bundle = new Bundle();
        bundle.putStringArrayList("list", (ArrayList<String>)list);

        fragment.setArguments(bundle);

        return fragment;
    }