jeudi 29 octobre 2015

How to show the item that you check when comeback in a DialogFragment

I have a DialogFragment that show me the days of the week and a checkbox. My question is simple (but I can't do it) How can I save my selections to when I open again the DialogFragment I can see the days that I enable the last time.

Because I can't do something like this mSelectedItems.add(which).setChecked(true).

Thanks in advance.

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();
    }




Aucun commentaire:

Enregistrer un commentaire