我有一个显示星期几的 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();
}
最佳答案
您正在设置为 null 检查值数组:
builder.setMultiChoiceItems(listitems, null, new DialogInterface.OnMultiChoiceClickListener());
检查 this .您需要做的是将选中的值保存为 SharedPreferences
中的一个集合。然后,在执行 setMultiChoiceItems
之前从 SharedPreference
带回这些值.
关于android - 如何在 DialogFragment 中显示您复出时检查的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33414714/