I’m working on an android app which at a point uses a DatePicker widget, butI’ve noticed that every time I try to change the date, the OnDateChange callback is called twice.
I could live with that if the new date was consistent on all devices, but I’ve just noticed that on emulator first is called with the good date but the second time with the old date.
this is the log printed within the callback:
12-18 20:29:12.285: E/MyOnDateChangeListener(5301): android.widget.DatePicker@43da7f90; year=2011; monthOfYear=11; dayOfMonth=19
12-18 20:29:12.355: E/MyOnDateChangeListener(5301): android.widget.DatePicker@43da7f90; year=2011; monthOfYear=11; dayOfMonth=18
Am I missing something here?
Please note that on the logs from the phone (Nexus S) both times I have the new date.
Here is the listener:
public class MyOnDateChangeListener implements OnDateChangedListener {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Log.e("MyOnDateChangeListener", view.toString() + "; year=" + year + "; monthOfYear=" + monthOfYear + "; dayOfMonth=" + dayOfMonth);
Date date = new Date();
date.setDate(dayOfMonth);
date.setMonth(monthOfYear);
date.setYear(year - 1900);
// TODO: modify the list content.
ListEventsByDate(date);
}
}
And here is the creation/register part:
currentDate = new Date();
currentDate.setDate(day);
currentDate.setMonth(month);
currentDate.setYear(year - 1900);
MyOnDateChangeListener onDateChangeListener = new MyOnDateChangeListener();
eventDatePicker.init(year, month, day, onDateChangeListener);
try {
eventDatePicker.setMinDate(currentDate.getTime() - DAY_IN_MILIS);
} catch (Throwable e) {
Log.e("ERR", "no MinDate method available");
};
I forgot to mention that if I write the date in the text fields it is ok, the issue I’m talking about is only when I hit the ‘+’ and ‘-‘ buttons.
I found the problem. It was because of my code. In ListEventsByDate() method I was changing the focus on the list below the DatePicker and that triggered the second call!