I\'m trying to create an app where the user selects a date from a DatePicker, and then a list is updated with some values.
My GUI looks like this:
Instead of the onDateSet callback I've overridden onClick method of the DatePickerDialog dialog and handle BUTTON_POSITIVE clicks only.
public static class DatePickerFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int initialYear = c.get(Calendar.YEAR);
int initialMonth = c.get(Calendar.MONTH);
int initialDay = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), null, initialYear, initialMonth, initialDay) {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == BUTTON_POSITIVE) {
int year = getDatePicker().getYear();
int month = getDatePicker().getMonth();
int day = getDatePicker().getDayOfMonth();
//TODO: Do your logic here
}
super.onClick(dialog, which);
}
};
}
}
The code stays clear but still it's not clear why onDateSet is called twice.
private DatePicker.OnDateChangedListener dateSetListener = new DatePicker.OnDateChangedListener() {
boolean fired = false;
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
if (fired == true){
//Twince
}
else{
Calendar c = Calendar.getInstance();
c.set(year, monthOfYear, dayOfMonth);
System.out.println ("TEST");
}
}
};
Please check my answer over here. It is for TimePickerDialog but also applicable for DatePickerDialog. You can create a custom dialog, it's almost equally as fast and simple and just works.
https://stackoverflow.com/a/25672760/940450
When I test my application, method onDateSet called twice after accept the date selection and once when I canceled.
I added a validation in the method onDateSet with parameter view, something like this
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth){
if (view.isShown()) {
updateDate(year, monthOfYear, dayOfMonth);
}
}
I hope you serve
private DatePicker.OnDateChangedListener dateSetListener = new DatePicker.OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Calendar c = Calendar.getInstance();
c.set(year, monthOfYear, dayOfMonth);
updateDisplay();
}
};
TextView datetext;
datetext = (TextView) findViewById(R.id.selected_date);
private void updateDisplay() {
this.datetext.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-").append(mDay).append("-")
.append(mYear).append(" "));
}
Perhaps modify the listener by adding instance variables, that way you can check to see if they are different from the last time the method was called:
final DatePickerDialog datePickerDialog = new DatePickerDialog (this, new DatePickerDialog.OnDateSetListener()
{
private int year;
private int month;
private int day;
@Override
public void onDateSet (DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
if (this.year == year && this.month == monthOfYear && this.day == dayOfMonth)
return;
this.year = year;
this.month = monthOfYear;
this.day = dayOfMonth;
calendar.set (year, month, day);
int index = officeCalendar.indexOfKey (calendar.getTimeInMillis ());
if (index > -1)
viewPager.setCurrentItem (index);
}
}, calendar.get (Calendar.YEAR), calendar.get (Calendar.MONTH), calendar.get (Calendar.DATE));