2014-03-25 169 views
0

在我的輪盤日期選擇器中,在28,29,30或31個月出現問題。現在,這個問題沒有被看到。檢查錯誤, (1)請在31.3.2014或30.1.2014中更改仿真器的日期....類似這樣的內容。 (2)運行代碼,你會看到二月和一些月份有什麼問題。 Here is the source code輪日期選擇器中的日期更改有問題

這裏是我的代碼...

Calendar updateDays(WheelView year, WheelView month, WheelView day) { 

     Calendar calendar = Calendar.getInstance(); 

     if(Number == 0){ 
      calendar.set(Calendar.YEAR,1900+year.getCurrentItem()); 
     }else{ 
      calendar.set(Calendar.YEAR,+curYear+year.getCurrentItem()); 
     } 

     calendar.set(Calendar.MONTH, month.getCurrentItem()); 

     int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); 

     //day.setViewAdapter(new DateNumericAdapter(Mcontex, 1, maxDays, calendar.get(Calendar.DAY_OF_MONTH) - 1)); 
     day.setViewAdapter(new NumericWheelAdapter(Mcontex, 1, maxDays, "%01d Day")); 

     int curDay = Math.min(maxDays, day.getCurrentItem() + 1); 

     day.setCurrentItem(curDay - 1, true); 
     calendar.set(Calendar.DAY_OF_MONTH, curDay); 

     Log.i("curDay",curDay+""); 
     Log.i("maxDays",maxDays+""); 
     Log.i("Calendar.DayOFMonth",Calendar.DAY_OF_MONTH+""); 

     return calendar; 

} 

回答

0

我只是解決了這個問題......如果有人得到了同樣的錯誤,這可能是幫助... :)

Calendar updateDays(WheelView year, WheelView month, WheelView day) { 

    Calendar calendar = Calendar.getInstance(); 
    int maxDays = 0 ; 

    if(Number == 0){ 
     calendar.set(Calendar.YEAR,1900+year.getCurrentItem()); 
    }else{ 
     calendar.set(Calendar.YEAR,+curYear+year.getCurrentItem()); 
    } 

    calendar.set(Calendar.MONTH, month.getCurrentItem()); 
    int YearChanging= 1900+ year.getCurrentItem(); 

    switch(month.getCurrentItem()){ 
    case 3: 
    case 5: 
    case 8: 
    case 10: maxDays= 30; break; 

    case 0: 
    case 2: 
    case 4: 
    case 6: 
    case 7: 
    case 9: 
    case 11: maxDays=31; break; 

    case 1: 
     if ((YearChanging % 400 == 0) || ((YearChanging % 4 == 0) && (YearChanging % 100 != 0))) { 
      maxDays =29 ; 
     } else { 
      maxDays = 28 ; 
     } 
     break; 
    } 

    day.setViewAdapter(new NumericWheelAdapter(Mcontex, 1, maxDays, "%01d Day")); 

    int curDay = Math.min(maxDays, day.getCurrentItem() + 1); 

    day.setCurrentItem(curDay - 1, true); 
    calendar.set(Calendar.DAY_OF_MONTH, curDay); 

    return calendar; 

}