vendredi 2 septembre 2016

Saving Checkbox dialog selected Items using SharedPreferences

I have a CheckBox dialog, everytime I select some items in the dialog then I close the dialog, then re-open the dialog, all the items I previously selected are gone.

This is my code

set_repeat_days = (LinearLayout)findViewById(R.id.repeat_days);
    set_repeat_days.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final ArrayList<Integer> mSelectedItems = new ArrayList();
            AlertDialog.Builder builder = new AlertDialog.Builder(SettingsActivity.this);

            String[] days = new String[]{"Monday", "Thursday", "Wednesday", "Tuesday", "Friday", "Saturday", "Sunday"};

            final boolean[] checkedDays = new boolean[]{true, true, true, true, true, true, true};

            builder.setMultiChoiceItems(days, checkedDays, new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int which, boolean isChecked) {

                    if (isChecked){
                        mSelectedItems.add(which);
                    }else if (mSelectedItems.contains(which)){
                        mSelectedItems.remove(Integer.valueOf(which));
                    }
                }
            });
            // Specify the dialog is not cancelable
            builder.setCancelable(false);
            builder.setTitle("Repeat");

            // Set the positive/yes button click listener
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });

            // Set the neutral/cancel button click listener
            builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            AlertDialog dialog = builder.create();
            dialog.show();
        }
    });




Aucun commentaire:

Enregistrer un commentaire