I have a DatePickerDialog, which I call in the following way:
DatePickerDialog dialog = new DatePickerDialog(EditActivity.this, mDateSetListener, 2015, 1, 10);
dialog.setButton(
DialogInterface.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_NEGATIVE) {
}
}
});
dialog.setButton(
DialogInterface.BUTTON_POSITIVE, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
... Save values from picker here...
}
}
});
dialog.show();
where the listener is:
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
}
};
The onDateSet
method is called every time... on OK, on CANCEL, on DISMISS (no button pressed).
I would only like the values to be registered and stored if the user presses "OK", and thus the DialogInterface.BUTTON_POSITIVE
. It works great, but is there an in-built method with pickerDialogs to get the values?
What I'm having to do right now is put a boolean inside the positive button method:
if(which == DialogInterface.BUTTON_POSITIVE){mOKClicked = true;}
and then test for it in the onDateSet
:
if(mOKClicked){... save values...}
As far as I can tell, the DialogInterface.BUTTON_POSITIVE
is called before the onDateSet
, but I'm not sure if it is always the case. I would prefer an in-built call to gather the values set in the picker, if such a thing exists.
Thanks in advance!