4
votes

I have defined DatePickerFragment in my android application, here is the code for it:

public class DatePickerFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {

    private int year;
    private int month;
    private int day;


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        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);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);

    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public Date getDateFromDatePicker(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, day);
        return calendar.getTime();
    }
}

I want to retrieve the date from this date picker from TaskFragment, and set the "date" attribute in my class Task to be equal to the date selected

        // Method for date picker to appear once the "Due Date" button is clicked.
    datePickerButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentManager fm = getActivity().getFragmentManager();
            DatePickerFragment datePicker = new DatePickerFragment();
            datePicker.show(fm, "datePicker");

            task.setDate(datePicker.getDateFromDatePicker());
           //test
           System.out.println(task.getDate());


        }
    });

The date picker appears when clicking the button. However, the date is not being set correctly, and is actually being set as soon as the date button is pressed, not when a date is selected from the picker. Also, when printing the date, I get the following no matter what date I select: I/System.out: Wed Dec 31 10:47:38 GMT+00:00 2 Reading some solutions here directed me to define the onDateSet and getDateFromDatePicker methods, but they do not seem to be functioning correctly. Any help is much appreciated, thank you!

2
Where are you showing the date picker, in an activity?FrankR
No in a fragment named TaskFragment.sums22
I have figured out that the call to the method getDateFromDatePicker happens before the onDateSet method is called, so I suppose I have to take the values from onDateSet and store them somewhere so that they can be accessed by the TaskFragment class.sums22

2 Answers

2
votes

After some digging, I followed the method I read in the book Android Programming: The Big Nerd Ranch Guide, that clearly specifies how to pass data between two Fragments. Here are the steps I followed:

  1. Setting a Target Fragment: Here I made the Fragment that will receive the date (TaskFragment in my case) the target fragment of the DatePickerFragment through the setTargetFragment method


    // set taskFragment as the target fragment of DatePickerFragment
    datePicker.setTargetFragment(TaskFragment.this, 0);
    datePicker.show(fm, "datePicker");


  1. Send date to the target Fragment through the DatePickerFragment's onDateSet method. I did this by passing an intent with the date to the target fragment's onActivityResult method.


    public void onDateSet(DatePicker view, int year, int month, int day) {
        // create date object using date set by user
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, day);
        Date date = calendar.getTime();

        if (getTargetFragment() == null)
            return;

        Intent intent = new Intent();
        intent.putExtra(DATE, date);

        // pass intent to target fragment
        getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intent);


  1. Override onActivityResult method in target fragment to accept date


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK){
            return;
        }

        if (requestCode == REQUEST_DATE){
            Date date = (Date) data.getSerializableExtra(DatePickerFragment.DATE);
            task.setDate(date);

        }
    }


0
votes

When using DatePickerFragment, you would normally have an interface that is triggered when the user has selected the date. This is called DatePickerDialog.OnDateSetListener.

In your code you are showing the picker and then setting the date straight away instead of waiting for the user to choose. So what you need to do is when the onDateSet callback method is triggered, do what you want with the date then.

If you are showing the DatePickerFragment in an Activity, one way of doing this would be to create an interface that allows sending messages from your Fragment to the activity.