문제

I am creating a DatePickerDialog, like it's done in the documentation. However, I am noticing that the CalendarView's title (i.e. "December 2012" as it would be for today) doesn't change immediately when the year is set in the Spinners. I see that the weeks are changed correctly, and I can set the title on the dialog based on the onSelectedDayChange callback with the appropriate date (month, month day, year, week day). Furthermore, if the month is changed in the Spinners, then the CalendarView is updated immediately. This includes, correctly showing the selected year if the year was changed before the month was changed. And if the CalendarView is scrolled to other months the year also gets adjusted to show the correct year.

This seems to imply that the CalendarView simply isn't redrawing the title (probably optimization?) when the date is getting set. Am I doing something else wrong? Is there a solution to this? Or is it a bug in the implementation?

Here's my code:

public class DatePickerFragment extends DialogFragment
    implements DatePickerDialog.OnDateSetListener {
    /** Name of the date stored in a {@link Bundle} */
    public static final String KEY_DATE = "key.DatePickerFragment.DATE";

    @Override
    public Dialog onCreateDialog(Bundle icicle) {
        final Bundle arguments = getArguments();
        Time date = arguments == null
                ? TimeMachine.getTimeFromArray(getArguments().getIntArray(KEY_DATE))
                : null;

        if (date == null)
            date = TimeMachine.getToday();

        _dialog_window = new DatePickerDialog(getActivity(), this, date.year, date.month, date.monthDay);
        final CalendarView calendar_view = _dialog_window.getDatePicker().getCalendarView();
        calendar_view.setOnDateChangeListener(
                new CalendarView.OnDateChangeListener() {
                    @Override
                    public void onSelectedDayChange(CalendarView _, int year, int month, int day) {
                        updateTitle(TimeMachine.getTimeFromArray(new int[]{ year, month, day }));
                    }
                }
        );

        // Sets the title
        updateTitle(date);

        // Create a new instance of DatePickerDialog and return it
        return _dialog_window;
    }

    @Override
    public void onDateSet(DatePicker _, int year, int month, int day) {
        final Time date = new Time();
        date.set(day, month, year);
    }

    private void updateTitle(Time date) {        
        _dialog_window.setTitle(date.format(" %A,  %B %e, %Y"));
    }

    /** The Dialog window */
    private DatePickerDialog _dialog_window;
}
도움이 되었습니까?

해결책 2

Seeing as there aren't others coming forth with any other answer, I got around this problem by not enabling the CalendarView on the DatePicker, and instead making a custom DialogFragment that had a numerical date picker and a separate CalendarView (actually my own reimplementation based on the real CalendarView to allow tweaks to it's display and Otto event bus). Then I can make sure that the CalendarView is set each time the numeric date picker is set.

다른 팁

I suffered from the same issue and submitted a bug report: http://code.google.com/p/android/issues/detail?id=53875

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top