Get and set data from a dialog box (DatePicker)

My DialogFragment class is called when I click the button in action. I want the date set through this dialog to appear in the button text. FindViewById refuses to recognize the class.

I found a similar question , but I cannot relate it to my case.

code:

  • Button that invokes a fragment of the dialog:

    public void datepicker(View v) { DialogFragment newFragment = new DatePickerFragment(); newFragment.show(getFragmentManager(), "datePicker"); } 
  • Code for the dialog fragment:

     public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { @Override public Dialog onCreateDialog(Bundle savedInstanceSateate) { final Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int day = c.get(Calendar.DAY_OF_MONTH); return new DatePickerDialog(getActivity(), this, year, month, day); } public void onDateSet(DatePicker view, int year, int month, int day) { // Do something with the date chosen } } } 
+3
source share
4 answers

In the method in your DialogFragment that is responsible for notifying when the user sets the date, do this

 Button activityButton = (Button)getActivity().findViewById(R.id.myButton); activityButton.setText (myDate); 
+10
source

Create a class for the DatePicker snippet:

  public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Calendar now = Calendar.getInstance(); year = now.get(Calendar.YEAR); month = now.get(Calendar.MONTH); day = now.get(Calendar.DATE); return new DatePickerDialog(getActivity(), this, year, month, day); } public void onDateSet(DatePicker view, int year, int month, int day) { orderDateEditText.setText(year + "-" + (month + 1) + "-" + day); } } 

Call this snippet When you click the ImageButton button:

  /** Create DatePicker dialog on click of OrderDateEditText **/ orderDatePickerButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub DialogFragment newFragment = new SelectDateFragment(); newFragment.show(getFragmentManager(), "DatePicker"); } }); 

There is plus 1 in a Month , because by default DatePicker accepts a month from 0 to 11 for the period from January to December.

Thanks..

+2
source

here is a nice template:

  • create a listener interface in the dialog box to handle the corresponding button clicks in the dialog box
  • realize it in the initial action
  • when the user clicks "okay" or "cancel", calls the listening methods
  • do everything you need to do in the listener module to update the activity view.

sort of,

 class MyDialogFragment extends DialogFragment implements DialogInterface.OnClickListener { static interface Listener { void onOkay(MyObject result); void onCancel(); } ... @Override public Dialog onCreateDialog(Bundle savedInstanceState) { ... // set this as a listener for ok / cancel buttons return new AlertDialog.Builder(activity)...whatever... .setPositiveButton(R.string.ok, this) .setNegativeButton(R.string.cancel, this).create(); } @Override public void onClick(DialogInterface dialog, int which) { if (which == DialogInterface.BUTTON_POSITIVE) { if (getActivity() instanceof Listener) { ((Listener)getActivity()).onOkay(...); } } else { if (getActivity() instanceof Listener) { ((Listener)getActivity()).onCancel(); } } } } class MyActivity extends Activity implements MyDialogFragment.Listener { ... @Override public void onOkay(MyObject result) { // update activity view here with result } @Override public void onCancel() { // anything? } } 

if your case, the β€œresult” passed to the onOkay() method will be the Date object selected by the user in the dialog box.

+1
source

A Fragment not a View . If you want to find the fragment, use findFragmentById from the FragmentManager .

0
source

Source: https://habr.com/ru/post/946357/


All Articles