Back to project page ExpertAndroid.
The source code is released under:
MIT License
If you think the Android project ExpertAndroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.iuriio.demos.expertandroid.ch2durationcontrol; // w w w. ja va2 s. c o m import android.app.DatePickerDialog; import android.app.Dialog; import android.app.DialogFragment; import android.os.Bundle; import android.view.View; import android.widget.DatePicker; import java.util.Calendar; public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { private DurationControl parent; private int buttonId; /** * Initializes a new instance of the DatePickerFragment. * * @param parent Parent control instance. * @param buttonId Target button Id. */ public DatePickerFragment(DurationControl parent, int buttonId) { this.parent = parent; this.buttonId = buttonId; Bundle argsBundle = this.getArguments(); if (argsBundle == null) { argsBundle = new Bundle(); } argsBundle.putInt("parentid", this.parent.getId()); argsBundle.putInt("buttonid", this.buttonId); this.setArguments(argsBundle); } /** * Initializes a new instance of the DatePickerFragment. */ public DatePickerFragment() { // Default constructor for device rotation. } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Calendar calendar = Calendar.getInstance(); final int year = calendar.get(Calendar.YEAR); final int month = calendar.get(Calendar.MONTH); final int day = calendar.get(Calendar.DAY_OF_MONTH); return new DatePickerDialog(this.getActivity(), this, year, month, day); } @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { this.parent.onDateSet(this.buttonId, year, monthOfYear, dayOfMonth); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); this.establishParent(); } @SuppressWarnings("ConstantConditions") private void establishParent() { if (this.parent != null) { return; } int parentId = this.getArguments().getInt("parentid"); int buttonId = this.getArguments().getInt("buttonid"); View view = this.getActivity().findViewById(parentId); if (view == null) { throw new RuntimeException("Cant establish parent on restart"); } this.parent = (DurationControl) view; this.buttonId = buttonId; } }