保存复选框对话框选中框

时间:2014-07-03 10:26:04

标签: android dialog android-checkbox

我有一个CheckBox对话框,每当我在对话框中选择一些值/项目然后关闭对话框,然后重新打开对话框时,我之前选择的所有值/项目都消失了。

我已经用Google搜索并查看了我能找到的每个帖子,从我看到的,使用SharePreferences将是最好的解决方案。我已经设置了每次选中checkBox时,它会在sharepreferences中存储一个值。现在,我仍然试图根据这些值来检查复选框。

任何指导都将不胜感激,谢谢。

这就是我已经拥有的,谢谢你看:

 AlertDialog dialog;

        final CharSequence[] items = { "All listings", "Buy it now",
                "Auction", "Nearest first" };
        // arraylist to keep the selected items
        final ArrayList<Integer> seletedItems = new ArrayList<Integer>();
        final SharedPreferences shfObject = getActivity()
                .getSharedPreferences("NAME", Context.MODE_PRIVATE);
        final SharedPreferences.Editor shfEditorObject = shfObject.edit();
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Filter");





        builder.setMultiChoiceItems(items,
                new boolean[] { true, false, false, false, false },
                new DialogInterface.OnMultiChoiceClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog,
                            final int indexSelected, boolean isChecked) {

                        if (isChecked) {



                            if (indexSelected == 0) {



                                seletedItems.add(indexSelected);



                                shfEditorObject.putString("checkbox1Ticked", "Ticked1");

                                shfEditorObject.commit();



                            }

                            if (indexSelected == 1) {

                                seletedItems.add(indexSelected);



                                shfEditorObject.putString("checkbox2Ticked", "Ticked2");

                                shfEditorObject.commit();
                            }

                            if (indexSelected == 2) {

                                seletedItems.add(indexSelected);



                                shfEditorObject.putString("checkbox3Ticked", "Ticked3");

                                shfEditorObject.commit();

                            }

                            if (indexSelected == 3) {




                                seletedItems.add(indexSelected);



                                shfEditorObject.putString("checkbox4Ticked", "Ticked4");

                                shfEditorObject.commit();




                            }

                            else if (seletedItems.contains(indexSelected)) {
                                // Else, if the item is already in the
                                // array, remove it
                                // write your code when user Uchecked the
                                // checkbox

                            }

                        }



                    }
                })
                // Set the action buttons
                .setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                // Your code when user clicked on OK
                                // You can write the code to save the
                                // selected item here

                                FILTER1 = shfObject.getString("FILTER",
                                        null);

                                FILTER2 = shfObject.getString("FILTER2",
                                        null);

                                Postcode = shfObject.getString("POSTCODE",
                                        null);

                                findItemsAdvanced(1,
                                        DEFAULT_ENTRIES_PER_PAGE, true,
                                        Postcode, "Citroen c1", FILTER1,
                                        FILTER2);

                            }
                        })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                // Your code when user clicked on Cancel

                            }
                        });

        dialog = builder.create();// AlertDialog dialog; create like this
                                    // outside onClick
        dialog.show();

0 个答案:

没有答案