java - Date/Time Pickers not giving changed values -
i created alertdialog contains both datepicker , timepicker inside of it. date , time pickers initialized each time opened whatever date on text of button used open it. when user clicks "set" button in dialog box supposed update text on button selected date , time keeps @ old date , time.
it appears using datepicker.getyear() , other methods isn't getting selected data in picker because made toast test , displayed old information only.
here code:
startdate.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //get date button text calendar timeinbutton = calendar.getinstance(); simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm", locale.us); try { timeinbutton.settime(sdf.parse(startdate.gettext().tostring())); } catch (parseexception e) { e.printstacktrace(); } int pyear = timeinbutton.get(calendar.year); int pmonth = timeinbutton.get(calendar.month); int pday = timeinbutton.get(calendar.day_of_month); int phour = timeinbutton.get(calendar.hour_of_day); int pminute = timeinbutton.get(calendar.minute); final view dialogview = view.inflate(myactivity.this, r.layout.date_picker_dialog, null); final datepicker datepicker = (datepicker) dialogview.findviewbyid(r.id.date_picker); datepicker.init(pyear, pmonth, pday, null); final timepicker timepicker = (timepicker) dialogview.findviewbyid(r.id.time_picker); timepicker.setcurrenthour(phour); timepicker.setcurrentminute(pminute); alertdialog.builder dateandtimepickerdialog = new alertdialog.builder(addcashgame.this); dateandtimepickerdialog.settitle("select starting date , time"); layoutinflater inflater = addcashgame.this.getlayoutinflater(); dateandtimepickerdialog.setview(inflater.inflate(r.layout.date_picker_dialog, null)); dateandtimepickerdialog.setpositivebutton("set", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { int newyear = datepicker.getyear(); int newmonth = datepicker.getmonth() + 1; int newday = datepicker.getdayofmonth(); int newhour = timepicker.getcurrenthour() % 12; string newminute = formatter.format(timepicker.getcurrentminute()); string newamorpm = timepicker.getcurrenthour() % 12 <= 12 ? " am" : " pm"; string newtime = newmonth + "/" + newday + "/" + newyear + " " + newhour + ":" + newminute + newamorpm; startdate.settext(newtime); toast.maketext(addcashgame.this, newtime + "",toast.length_short).show(); } }); dateandtimepickerdialog.setnegativebutton("cancel", null); dateandtimepickerdialog.show(); } });
this happening because you're pointing alertdialog
view
new one, instead of 1 you've inflated in line:
final view dialogview = view.inflate(myactivity.this, r.layout.date_picker_dialog, null);
the first view you've inflated contains both datepicker
, timepicker
set right time, while new 1 set, default, calendar.getinstance()
.
change line:
dateandtimepickerdialog.setview(inflater.inflate(r.layout.date_picker_dialog, null));
to line:
dateandtimepickerdialog.setview(dialogview);
Comments
Post a Comment