android - Awesome Errors with date wheel picker? -
i have problem date wheel picker. has change date specific month. in 30, february doesn't have 28 days. in 31, months change 31. days, works pretty well.(eg. in emulator date 1-2-2014, work yesterday, emulator date 31-1-2014, months 31.) , problem, in jun 30, february doesn't have 28.what should do, give me advices.
package com.example.aa; import java.util.arraylist; import java.util.calendar; import java.util.list; import android.app.dialog; import android.content.context; import android.graphics.typeface; import android.util.log; import android.view.view; import android.view.viewgroup; import android.view.window; import android.widget.button; import android.widget.linearlayout; import android.widget.tablelayout.layoutparams; import android.widget.textview; import kankan.wheel.widget.adapters.arraywheeladapter; import kankan.wheel.widget.adapters.numericwheeladapter; import kankan.wheel.widget.onwheelchangedlistener; import kankan.wheel.widget.wheelview; public class datepickerdailog extends dialog { private context mcontex; private int noofyear = 113; public datepickerdailog(context context, calendar calendar, final datepickerlistner dtp) { super(context); mcontex = context; linearlayout lytmain = new linearlayout(mcontex); lytmain.setorientation(linearlayout.vertical); linearlayout lytdate = new linearlayout(mcontex); linearlayout lytbutton = new linearlayout(mcontex); button btnset = new button(mcontex); button btncancel = new button(mcontex); btnset.settext("set"); btncancel.settext("cancel"); final wheelview month = new wheelview(mcontex); month.setminimumheight(300); final wheelview year = new wheelview(mcontex); year.setminimumheight(300); final wheelview day = new wheelview(mcontex); day.setminimumheight(300); lytdate.addview(day, new layoutparams( android.view.viewgroup.layoutparams.wrap_content, android.view.viewgroup.layoutparams.wrap_content, 1.2f)); lytdate.addview(month, new layoutparams( android.view.viewgroup.layoutparams.wrap_content, android.view.viewgroup.layoutparams.wrap_content, 0.8f)); lytdate.addview(year, new layoutparams( android.view.viewgroup.layoutparams.wrap_content, android.view.viewgroup.layoutparams.wrap_content, 1f)); requestwindowfeature(window.feature_no_title); lytbutton.addview(btnset, new layoutparams( android.view.viewgroup.layoutparams.fill_parent, android.view.viewgroup.layoutparams.wrap_content, 1f)); lytmain.addview(lytbutton); lytmain.addview(lytdate); setcontentview(lytmain); getwindow().setlayout(layoutparams.fill_parent, layoutparams.wrap_content); getwindow().setbackgrounddrawableresource(android.r.color.white); onwheelchangedlistener listener = new onwheelchangedlistener() { public void onchanged(wheelview wheel, int oldvalue, int newvalue) { updatedays(year, month, day); } }; // month int curmonth = calendar.get(calendar.month); log.i("curmonth",curmonth+""); string months[] = new string[] { "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december" }; month.setviewadapter(new datearrayadapter(context, months, curmonth)); month.setcurrentitem(curmonth); month.addchanginglistener(listener); calendar cal = calendar.getinstance(); // year int curyear = calendar.get(calendar.year); log.i("curyear", curyear+""); int year = cal.get(calendar.year); arraylist<integer> yearlist = new arraylist<integer>(); int [] yeararray=null; for(int = 1900 ; <= curyear ; i++){ yearlist.add(i); } yeararray = new int[yearlist.size()]; for(int = 0 ; < yeararray.length ;i++){ yeararray[i] = yearlist.get(i); } year.setviewadapter(new datenumericadapter(context,1900,curyear, curyear)); year.setcurrentitem(curyear-1900); year.addchanginglistener(listener); // day updatedays(year, month, day); day.setcurrentitem(calendar.get(calendar.day_of_month) - 1); log.i("calendar.get(calendar.day_of_month) - 1",calendar.get(calendar.day_of_month) - 1+""); btnset.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub calendar c = updatedays(year, month, day); dtp.ondonebutton(datepickerdailog.this, c); } }); btncancel.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { dtp.oncancelbutton(datepickerdailog.this); } }); } calendar updatedays(wheelview year, wheelview month, wheelview day) { calendar calendar = calendar.getinstance(); calendar.set(calendar.year,1900+year.getcurrentitem()); calendar.set(calendar.month, month.getcurrentitem()); log.i("month.getcurrentitem()",month.getcurrentitem()+""); int maxdays = calendar.getactualmaximum(calendar.day_of_month); log.i("maxdays",maxdays+""); day.setviewadapter(new datenumericadapter(mcontex, 1, maxdays, calendar.get(calendar.day_of_month) - 1)); int curday = math.min(maxdays, day.getcurrentitem() + 1); day.setcurrentitem(curday - 1, true); calendar.set(calendar.day_of_month, curday); //calendar.set(calendar.day_of_month, day.getcurrentitem()+1); log.i("calendar",calendar+""); long millicalendar = calendar.gettimeinmillis(); calendar currentcalendar = calendar.getinstance(); long millicurrentcalendar = currentcalendar.gettimeinmillis(); if( millicalendar > millicurrentcalendar){ return currentcalendar; } else{ return calendar; } } private class datenumericadapter extends numericwheeladapter { int currentitem; int currentvalue; public datenumericadapter(context context, int minvalue, int maxvalue, int current) { super(context, minvalue, maxvalue); this.currentvalue = current; settextsize(20); } @override protected void configuretextview(textview view) { super.configuretextview(view); view.settypeface(null, typeface.bold); } @override public view getitem(int index, view cachedview, viewgroup parent) { currentitem = index; return super.getitem(index, cachedview, parent); } } private class datearrayadapter extends arraywheeladapter<string> { int currentitem; int currentvalue; public datearrayadapter(context context, string[] items, int current) { super(context, items); this.currentvalue = current; settextsize(23); } @override protected void configuretextview(textview view) { super.configuretextview(view); if (currentitem == currentvalue) { } view.settypeface(null, typeface.bold); } @override public view getitem(int index, view cachedview, viewgroup parent) { currentitem = index; return super.getitem(index, cachedview, parent); } } public interface datepickerlistner { public void ondonebutton(dialog datedialog, calendar c); public void oncancelbutton(dialog datedialog); } }
the culprit here
calendar cal = calendar.getinstance();
i fixed issue using joda time instead of standard java.util.calendar/date
gregorianchronology calendar = gregorianchronology.getinstance(); datetimefield field = calendar.dayofmonth(); localdate date = new localdate(myear, mmonth+1, 1, calendar); int max = field.getmaximumvalue(date);
Comments
Post a Comment