【问题标题】:Persist AlertDialog checked optionsPersist AlertDialog 选中的选项
【发布时间】: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


【解决方案1】:

要小心,因为您将Integers 保存在首选项中,然后将ArrayListIntegers 转换为ArrayListStrings。这可能会导致Exception。在任何地方使用ArrayListsStrings

您只缺少在显示DialogFragment 之前必须阅读您保存的选择并在DialogFragment 中检查它们的部分。

有最简单的方法可以做你想做的事,但让我们采用这种方法:

正如您所注意到的,setMultiChoiceItems() 方法接收 String[] 作为您的 DialogFragmentboolean [] 的项目,以定义它们是否被检查。我们将使用这个数组来持久化我们的选择。此数组必须与 items 数组具有相同的长度,并且最初将设置为 false。

在您第一次启动 DialogFragment 时,不会检查任何项目。第二次,当您已经检查了项目时,您将从 TinyDB 中读取这些选项并使用 for 语句填充布尔数组。然后将其提供给 DialogFragment 构建器。

如您所愿,已保存的项目将在DialogFragment 上显示为选中状态。

这是完整的更改和工作代码:

    public class TimelineSettings extends DialogFragment {
    ArrayList<String> selected_categories = new ArrayList<String>();
    final String[]items = {"Fourniture","Nourriture","Voyages","Habillement","Médias","Autres"};
    boolean[] itemsChecked = {false, false, false, false, false, false};
    TinyDB tinydb;
    private SharedPreferences sharedPreference;
    private SharedPreferences.Editor sharedPrefEditor;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        tinydb = new TinyDB(getActivity());
        selected_categories = tinydb.getList("selected");

        for(int i=0;i<itemsChecked.length;i++){
            if(selected_categories.contains((String)String.valueOf(i)))
                    itemsChecked[i]=true;
        }

        // Set the dialog title
        builder.setTitle("Choisissez vos paramètres")
                .setMultiChoiceItems(items, itemsChecked,
                        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
                                    if(!selected_categories.contains((String)String.valueOf(indexselected))){
                                        selected_categories.add(String.valueOf(indexselected));
                                        itemsChecked[indexselected]=true;
                                    }
                                } else if (selected_categories.contains((String)String.valueOf(indexselected))) {
                                    // Else, if the item is already in the array, remove it
                                    selected_categories.remove((String)String.valueOf(indexselected));
                                    itemsChecked[indexselected]=false;
                                }
                            }
                        })
                        // 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();
    }
}

【讨论】:

  • 感谢您的回复,我确实有一些关于类型的错误(尝试将我的数组转换为集合时..)。现在我正在尝试使用您的核心,但是当我创建 tinydb 对象时出现空指针异常:TinyDB tinydb = new TinyDB(context); '我哪里出错了?这是我的班级,评论中有错误:gist.github.com/RidouaneHicham/f7e07445a76a314d7c0b
  • 您的上下文变量为空,因此您收到的 NPE。您必须为 TinyDB 构造函数提供有效的上下文。尝试使用 TinyDB tinydb = new TinyDB(getActivity());
  • 我在发表评论之前尝试过..仍然是 NPE :(
  • 您能发布您的 StackTrace 吗?
  • @RidouaneHicham 显然,对于不为空的Activity 上下文,您必须仅在创建对话框时提取它。如果你之前这样做,它是null,调用将返回null。
猜你喜欢
  • 1970-01-01
  • 2014-05-30
  • 1970-01-01
  • 1970-01-01
  • 2013-11-29
  • 1970-01-01
  • 1970-01-01
  • 2019-02-04
  • 2017-09-27
相关资源
最近更新 更多