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();
}
an optional parameter can be done like this:
You can call
newInstance()
if you have no number ornewInstance(300)
if you have.On the other side:
Alternatively instead of using
-1
you could not add theInt
at all and just check for the default value (I think its 0 in the API docs)