【发布时间】:2014-12-16 09:44:57
【问题描述】:
我已经为此苦苦挣扎了两个多星期。一直在讨论有关 sharedpreferenes 和其他“黑客”的所有 SO 问题,以坚持多选警报对话框。但不幸的是我仍然无法让它工作。
有人可以向我解释一下如何使这件事起作用吗?我的多项选择警报对话框有效。但我仍然无法保存所选项目..
我的代码:
public class TimelineSettings extends DialogFragment {
Context context;
final ArrayList selected_categories = new ArrayList();
final String[]items = {"Fourniture","Nourriture","Voyages","Habillement","Médias","Autres"};
TinyDB tinydb = new TinyDB(context);
private SharedPreferences sharedPreference;
private SharedPreferences.Editor sharedPrefEditor;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Set the dialog title
builder.setTitle("Choisissez vos paramètres")
.setMultiChoiceItems(items, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int indexselected,
boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
selected_categories.add(indexselected);
} else if (selected_categories.contains(indexselected)) {
// Else, if the item is already in the array, remove it
selected_categories.remove(Integer.valueOf(indexselected));
}
}
})
// Set the action buttons
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
tinydb.putList("selected",selected_categories);
}
})
.setNegativeButton("Annuler", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
});
return builder.create();
}
}
感谢您的帮助。
PS:我遇到了这个answer,如果你能向我解释如何使这个工作,那就太好了。
【问题讨论】:
-
当您第二次启动对话框时,您的选择没有设置,是吗?
-
您一开始并没有将任何内容保存到共享首选项中。构建对话框时,您也不会从共享首选项中检索任何内容。您也没有设置在构建时选择的选项。那么到目前为止你尝试过什么?您也没有从数据库中检索信息。也不保存数据库的状态。
-
@greenapps 我首先尝试将我的 arraylist 存储在 sharedprefs 上,为此我尝试将我的 arraylist 转换为一组字符串..但这不起作用..然后我尝试使用这个 tinydb类,但我再次无法使其工作..我的问题是我不确切知道如何使用 sharedprefs 来存储然后检索我的选定选项数组。你能告诉我怎么做吗?
-
@joao2fast4u 是的 :)
-
点击确定按钮是否出现崩溃?
标签: android arraylist sharedpreferences android-alertdialog