-2

I've been following the official android developer docu and this one for the use of DialogFragment inside my project. so far so good, since I have to pass data to the DialogFragment for it to create a multichoice list, I call the DialogFragment from inside my MainActivity via the newInstance method (passing the items) and I am getting a correct result. Now, I would like to pass another argument, also data for the DialogFragment but it has to be optional since I dont need to pass it all the time. Is there a way for me to achieve this?

EDIT:
so I took the Advice from the comments below and created a setter and passed the items i wished to pass to the DiagramFragment. It worked just fine, sadly it didn't help me solve my problem. The reason I wanted to pass the second data is that I thought, if the user opens the DialogFragment and makes a selection and after that reopens the DialogFragment his last choice is gone. I wanted to check the checkboxes he already had checked programmatically, by passing the checked once back to the DialogFragment and then setting the right indexes back into mSelectedItems - but even tho the indexes are set correctly the checkboxes stay unchecked.. is there a workaround?

static MyDialogFragment newInstance(int num) {
        MyDialogFragment f = new MyDialogFragment();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("num", num);
        f.setArguments(args);

        return f;
}

...

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    mSelectedItems = new ArrayList();  // Where we track the selected items
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Set the dialog title
    builder.setTitle(R.string.pick_toppings)
    // Specify the list array, the items to be selected by default (null for none),
    // and the listener through which to receive callbacks when items are selected
           .setMultiChoiceItems(R.array.toppings, 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);
                   } else if (mSelectedItems.contains(which)) {
                       // Else, if the item is already in the array, remove it
                       mSelectedItems.remove(Integer.valueOf(which));
                   }
               }
           })
    // Set the action buttons
           .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   // User clicked OK, so save the mSelectedItems results somewhere
                   // or return them to the component that opened the dialog
                   ...
               }
           })
           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   ...
               }
           });

    return builder.create();
}
b101
  • 379
  • 5
  • 15
  • 1
    standard setters perhaps –  Jan 01 '17 at 18:46
  • 1
    How about just passing null? When it's not available – Oleg Bogdanov Jan 01 '17 at 18:56
  • @JawadLeWywadi that is possible? how would I use that setter function `DialogFragment newFragment = MyDialogFragment.newInstance(items);` and then `newFragment.setMySetter(value);` ? like this ?? – b101 Jan 01 '17 at 19:21
  • @OlegBogdanov will try it out. If it works, its fine with me – b101 Jan 01 '17 at 19:22
  • well i used both of your advises and they work, but saddly didn't resolve my Problem. I'll edit my quesition and give more detail about it.. – b101 Jan 01 '17 at 20:10
  • SHOW US SOME CODE MAN – Blundell Jan 01 '17 at 20:22
  • @Blundell like I said i followed the android docu and the code above is basically the code I used. Turn your shitty caps lock off – b101 Jan 01 '17 at 20:27
  • _clicks link_ doesn't see any `newInstance` methods, CTL+F's for `newInstance` finds `0` – Blundell Jan 01 '17 at 20:28
  • also in the code you added I can't see a `newInstance` or the optional params you are talking about. It's pretty easy to do but I'm struggling to see where you are trying to do it – Blundell Jan 01 '17 at 20:31

2 Answers2

5

an optional parameter can be done like this:

static MyDialogFragment newInstance() {
        return newInstance(-1);
}

static MyDialogFragment newInstance(int num) {
        MyDialogFragment f = new MyDialogFragment();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("num", num);
        f.setArguments(args);

        return f;
}

You can call newInstance() if you have no number or newInstance(300) if you have.

On the other side:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
     int num = getArguments().getInt("num");
     if(num == -1) {
       // no extra number
     } else {
       // do something with the extra number
     }

...

Alternatively instead of using -1 you could not add the Int at all and just check for the default value (I think its 0 in the API docs)

Blundell
  • 75,855
  • 30
  • 208
  • 233
-2

I got it, it was already answered - funny I didn't find it when I googled..

There are easiest ways of doing what you want but let's take this approach: As you have noticed, the setMultiChoiceItems() method receives a String[] as the items of your DialogFragment and a boolean [] to define if they are checked or not. We are going to use this array to persist our choices. This array must have the same length as the items array and will be initially set to false.

So all I had to do was create a boolean[] itemsChecked and set the correct indexes to true, to re-check the right checkboxes..

Original Question Persist AlertDialog checked options - all credit to @joao2fast4u

Community
  • 1
  • 1
b101
  • 379
  • 5
  • 15