我已经创建了一个日历,其中suer可以滚动浏览前一个月和下一个月的日历,但我遇到的问题是用户尝试滚动到前一个月。Java日历日期月份设置不正确
当用户第一次点击到前一个月运行应用程序时,它可以工作,但是当用户再次点击它时,它并不会妨碍我发送给Calendar.Set()的值100%正确甚至调试但实际的日历不会更新,因此会返回与当前日期相同的月份!
这里是我的代码如下。
@Override
public void onClick(View v) {
// get current month
int currentMonth = mCurrentMonth.get(Calendar.MONTH);
Log.d(TAG, "day = " + mCurrentMonth.get(Calendar.DAY_OF_MONTH));
Log.d(TAG, "currentMonth in onClick = " + currentMonth);
if (v == mPreviousMonthButton) {
Log.d(TAG, "mPreviousMonthButton CLICKED ");
// if current month is january
// decrement the current year and set month to december
if (currentMonth == Calendar.JANUARY) {
int currentYear = mCurrentMonth.get(Calendar.YEAR);
mCurrentMonth.set(Calendar.YEAR, currentYear - 1);
mCurrentMonth.set(Calendar.MONTH, Calendar.DECEMBER);
} else {
// else decrement the month
Log.d(TAG, "currentMonth-- = " + currentMonth);
mCurrentMonth.set(Calendar.MONTH, currentMonth);
Log.d(TAG,
"month in previus button = "
+ mCurrentMonth.get(Calendar.MONTH));
}
// save the month
setDateForMonth();
} else if (v == mNextMonthButton) {
Log.d(TAG, "mNextMonthButton CLICKED ");
if (currentMonth == Calendar.DECEMBER) {
int currentYear = mCurrentMonth.get(Calendar.YEAR);
mCurrentMonth.set(Calendar.YEAR, currentYear + 1);
mCurrentMonth.set(Calendar.MONTH, Calendar.JANUARY);
} else {
currentMonth--;
mCurrentMonth.set(Calendar.MONTH, currentMonth + 1);
Log.d(TAG, "currentMonth++ = " + currentMonth + 1);
Log.d(TAG,
"month in next button = "
+ mCurrentMonth.get(Calendar.MONTH));
}
// save the month
setDateForMonth();
}
}
这里是实际更新UI的代码。问题是somwhere在onclick,因为它返回错误的月份在下面的代码:
私人无效setDateForMonth(){
monthList.clear();
Log.d(TAG, ".........setDateForMonth...........");
Log.d(TAG, "....................");
Log.d(TAG, "month = " + mCurrentMonth.get(Calendar.MONTH));
Log.d(TAG, "year = " + mCurrentMonth.get(Calendar.YEAR));
CalendarMonth[] months = CalendarUtils
.constructMonthViewArray(mCurrentMonth);
for (int i = 0; i < months.length; i++) {
monthList.add(months[i]);
Log.d(TAG, monthList.get(i).getDay());
}
Log.d(TAG, "....................");
mAdapter = new CalendarMonthAdapter(mContext, monthList);
mMonthGridView.setAdapter(mAdapter);
Months[] month = Months.values();
String currentMonth = month[mCurrentMonth.get(Calendar.MONTH)]
.toString();
String year = Integer.toString(mCurrentMonth.get(Calendar.YEAR));
mMonthLabel.setText(currentMonth + " " + year);
}
私人枚举月{一月, 二月,三月,四月,五月,六月, 七月,八月,九月,十月, 十一月,十二月};
这不是你如何发布代码到Stackoverflow。每行代码应该缩进4个空格。 – Pointy 2011-01-10 17:26:05
现在修正了队友谢谢 – jonney 2011-01-10 17:30:40