android - How to get datepicker properties from dialogfragment -
hello have code
public class datetimepicker extends android.support.v4.app.dialogfragment { public interface datetimepickerlistener { // public void ondialogpositiveclick(dialogfragment dialog); // public void ondialognegativeclick(dialogfragment dialog); public void ondialogpositiveclick(android.app.dialogfragment dialog); public void ondialognegativeclick(android.app.dialogfragment dialog); } // use instance of interface deliver action events datetimepickerlistener mlistener; datepicker dp; timepicker tp; int year; @override public dialog oncreatedialog(bundle savedinstancestate) { alertdialog.builder builder = new alertdialog.builder(getactivity()); // layout inflater layoutinflater inflater = getactivity().getlayoutinflater(); dp = (datepicker) this.getactivity().findviewbyid(r.id.date_picker); // tp = (timepicker) this.getview().findviewbyid(r.id.time_picker); // inflate , set layout dialog // pass null parent view because going in dialog layout builder.setview(inflater.inflate(r.layout.date_time_picker, null)) // add action buttons .setpositivebutton(r.string.set, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int id) { // sign in user ... year = dp.getdayofmonth(); stringbuilder sb = new stringbuilder(); sb.append(year); /* int day = dp.getdayofmonth(); int month= dp.getmonth()+1; int year = dp.getyear(); simpledateformat sdf = new simpledateformat("dd-mm-yyyy"); date dt = new date(day, month, year); string formateddate = sdf.format(dt);*/ serviceactivity.tv.settext(sb.tostring()); toast.maketext(getactivity().getbasecontext(), "you selected set button ", toast.length_long).show(); } }) .setnegativebutton(r.string.cansel, new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { datetimepicker.this.getdialog().cancel(); } }); return builder.create(); } // override fragment.onattach() method instantiate datetimepickerlistener @override public void onattach(activity activity) { super.onattach(activity); // verify host activity implements callback interface try { // instantiate noticedialoglistener can send events host mlistener = (datetimepickerlistener) activity; } catch (classcastexception e) { // activity doesn't implement interface, throw exception throw new classcastexception(activity.tostring() + " must implement datetimepickerlistener"); } } }
when click on set button program crashes. tryed
dp = (datepicker) this.getview().findviewbyid(r.id.date_picker);
it crashes tried
dp = (datepicker) this.dialog.findviewbyid(r.id.date_picker);
it crashes why wrong?
try code in oncreatedialog
:
layoutinflater inflater = layoutinflater.from(getactivity()); view view = inflater.inflate(r.layout.date_time_picker, null ); dp = (datepicker) view.findviewbyid(r.id.datepicker); builder.setview(view, null))
Comments
Post a Comment