sums22 sums22 - 4 years ago 205
Android Question

Get Date object from DatePickerFragment in Android

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!

Answer Source

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);

        }
    }


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download