Android Datepicker - How to pass date from activity to fragment

拥有回忆 提交于 2020-01-13 05:41:28

问题


All documentation & example seems to be related to "How to get the date selected from the datepicker dialog back in the activity". And almost all of them sets the current date as default date in the datepicker dialog. However how do I pass a specific date so that when the datepicker opens, it shows that date & not current date?

The problem I'm facing is, when the user clicks a button and the datepicker diaglog opens for the first time, it shows the current time.

The user changes the value and clicks done.

When the button is clicked again, the datepicker dialog is opened. However instead of the changed date, the value is still the default date!!

Code:

public class DatePickerFragment extends SherlockDialogFragment
        implements android.app.DatePickerDialog.OnDateSetListener{

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        //TODO Use current date is date null, else show currently displayed date

        // 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 monthOfYear,
            int dayOfMonth) {
        ((NewTransactionActivity) getActivity()).updateDate(year, monthOfYear, dayOfMonth);

    }

}

The above code is the reason for the datepicker always showing up with default date. However how do I pass the date value to this fragment?

This is code from the activity:

public void showDatePickerDialog(View v) {
    Bundle currentDate = new Bundle();
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker");
}

回答1:


you need two things:

1) to pass an input parameter (or parameters to the DataPickerFragment)..

Bundle args = new Bundle();
args.putInt("someInt", someInt);
args.putString("someString", someString);
// Put any other arguments
myFragment.setArguments(args);

2) to set the dialog date (please note that months start at 0 and end at 11)

Here is an example of it done using a dialog

private void showEditDatePopup()
{
    String dateString = mBirthDate.getText().toString();
    java.util.Date inputDate  = null;
    Calendar newCalendar = Calendar.getInstance();
    if (dateString.length() > 0)
    {
        try
        {
            inputDate = DateFormat.getDateInstance(DateFormat.MEDIUM).parse(dateString);
            newCalendar = Calendar.getInstance();
            newCalendar.setTime(inputDate);
        } catch (Exception e)
        {
            newCalendar = Calendar.getInstance();
            newCalendar.setTime(inputDate);
        }
    }


    DatePickerDialog dateDialog = new DatePickerDialog(this, new OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            Calendar newDate = Calendar.getInstance();
            newDate.set(year, monthOfYear, dayOfMonth);
            mBirthDate.setText(DateFormat.getDateInstance(DateFormat.MEDIUM).format(newDate.getTime()));
        }

    },newCalendar.get(Calendar.YEAR)
     ,newCalendar.get(Calendar.MONTH)
     ,newCalendar.get(Calendar.DAY_OF_MONTH));

    dateDialog.show();
}



回答2:


       //Get the date on the button now
        Date buttonDate = SimpleDateFormat.getDateInstance().parse(mDate.getText().toString());

        //Create a bundle to pass the date
        Bundle currentDate = new Bundle();
        currentDate.putLong("setDate", buttonDate.getTime());

        //Pass the bundle to the fragment
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.setArguments(currentDate);
        newFragment.show(getSupportFragmentManager(), "datePicker");

And now in the fragment do this:

    //Read the passed bundle from the activity
Bundle setDate = this.getArguments();
Long currDate = setDate.getLong("setDate");

// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
c.setTimeInMillis(currDate);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);


来源:https://stackoverflow.com/questions/16598629/android-datepicker-how-to-pass-date-from-activity-to-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!