micnguyen
micnguyen

Reputation: 1449

Datepicker not being updated

So here's something weird.

I have 3 elements: An EditText, a Spinner and a DatePicker. Whenever a value in TextEdit changes or when a Spinner value is selected, the DatePicker gets updated accordingly. Specifically, the Spinner has the choice of Months/Weeks/Days, so the DatePicker updates X weeks/days/months depending on the value in EditText. Simple enough... but I'm getting some very odd behaviour.

I have a listener on spinner & text field that invokes a static updateDatePicker() method on any change. Everything works fine on the text field change but for the spinner whenever I update from index 0 to index 2 (from months to days), the date picker does NOT get updated - it stays the same date. The odd thing is, if I go from 0 to 1 to 2, or 2 to 1 to 0 it works and any transition to/from index 1 works. I don't check for any special transitions, the updateDatePicker() method simlply does a switch case on the selectedIndexPosition of the spinner and does it's work accordingly.

I know the actual calculation is correct because I can print out the new date I want but it's just the datepicker that's not getting updated. Here is my Logcat and the gode that generates the logcat (where the extra "swapped" tag is just called when the method is called):

    Log.d("Updated Retest Date:", newDate.get(Calendar.YEAR) + " " + (newDate.get(Calendar.MONTH)-1) + " " + newDate.get(Calendar.DATE));
    // Month is starting from 0 hence -1 on the Month
    datePicker.updateDate(newDate.get(Calendar.YEAR), (newDate.get(Calendar.MONTH)-1), newDate.get(Calendar.DATE));
    Log.d("New Date:", datePicker.getYear() + " " + datePicker.getMonth() + " " + datePicker.getDayOfMonth());

12-03 13:42:56.240: D/DASDSAD(22299): swapped
12-03 13:42:56.240: D/Item Position Spinner(22299): 0
12-03 13:42:56.240: D/Updated Retest Date:(22299): 2013 9 8
12-03 13:42:56.250: D/New Date:(22299): 2013 9 8
12-03 13:43:00.140: D/DASDSAD(22299): swapped
12-03 13:43:00.140: D/Item Position Spinner(22299): 1
12-03 13:43:00.140: D/Updated Retest Date:(22299): 2013 8 15
12-03 13:43:00.160: D/New Date:(22299): 2013 8 15
12-03 13:43:01.985: D/DASDSAD(22299): swapped
12-03 13:43:01.990: D/Item Position Spinner(22299): 2
12-03 13:43:01.990: D/Updated Retest Date:(22299): 2013 8 9
12-03 13:43:02.000: D/New Date:(22299): 2013 8 9
12-03 13:43:03.625: D/DASDSAD(22299): swapped
12-03 13:43:03.625: D/Item Position Spinner(22299): 1
12-03 13:43:03.625: D/Updated Retest Date:(22299): 2013 8 15
12-03 13:43:03.630: D/New Date:(22299): 2013 8 15
12-03 13:43:04.900: D/DASDSAD(22299): swapped
12-03 13:43:04.905: D/Item Position Spinner(22299): 0
12-03 13:43:04.905: D/Updated Retest Date:(22299): 2013 9 8
12-03 13:43:04.915: D/New Date:(22299): 2013 9 8
12-03 13:43:06.255: D/DASDSAD(22299): swapped
12-03 13:43:06.260: D/Item Position Spinner(22299): 2
12-03 13:43:06.260: D/Updated Retest Date:(22299): 2013 8 9
12-03 13:43:06.260: D/New Date:(22299): 2013 9 8

As you can see on the last 3 lines, I get 2013 8 9 as the new date, update the datepicker with it and query the datepicker's new date but it still gives me 2013 9 8.

Thanks, I've been going crazy over this.

Upvotes: 0

Views: 1808

Answers (2)

Ervinne Sodusta
Ervinne Sodusta

Reputation: 1

You should not use newDate.get(Calendar.DATE) for getting the day. You can get rid of this ugly fix by changing Calendar.DATE to Calendar.DAY_OF_MONTH.

datePicker.updateDate(newDate.get(Calendar.YEAR), newMonth, newDate.get(Calendar.DAY_OF_MONTH));

Upvotes: 0

micnguyen
micnguyen

Reputation: 1449

So... I managed to fix the problem, but with a solution that isn't exactly elegant nor does it make sense. I'll leave my fix here for anyone that comes across something similar.

final int newMonth = newDate.get(Calendar.MONTH);
        Log.d("new month:", newMonth + "");

        // weird workaround some bug. LEAVE IT.
        datePicker.updateDate(newDate.get(Calendar.YEAR), newMonth , newDate.get(Calendar.DATE));
        datePicker.updateDate(newDate.get(Calendar.YEAR), datePicker.getMonth()-1,  newDate.get(Calendar.DATE));

Basically, updating the date wasn't working. So I had to set it up so that it updated twice, with the 2nd time using it's supposed "updated" value. I assume it was some weird memory thing... why that works for only those certain transitions I said before, who knows. shrug

Upvotes: 1

Related Questions