gpt4 book ai didi

android - 如何将 arrayList 传递给 AlertDialog fragment

转载 作者:行者123 更新时间:2023-11-30 00:48:50 25 4
gpt4 key购买 nike

所以我有一个 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();
}
}

我从我的主要 Activity 中调用它如下

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

但是,我想发送一个List<string>对此AlertDialog然后利用这个列表。

我不知道如何实现它。

感谢您的帮助

最佳答案

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

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

现在在newInstance() methodCustomDayRepeatFragment类,您可以将列表保存为 Bundle 中的参数作为bundle.putParcelableArrayList("list", list);然后从 onCreateDialog() method 中的包参数中检索它作为List<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;
}

关于android - 如何将 arrayList 传递给 AlertDialog fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41383589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com