如何从ChildViewHolder中的DialogFragment获取数据?

时间:2016-11-04 11:35:50

标签: android android-dialogfragment android-datepicker

我有可扩展的回收站视图。 在这个回收站视图中,我有ChildViewHolder,其中调用了以下方法:

public void showDatePickerDialog() {
    DatePickerFragment datePickerFragment= new DatePickerFragment();
    SelectionActivity activity = (SelectionActivity) mContext;
    datePickerFragment.show(activity.getSupportFragmentManager(), "datePicker");
}

我的DatePickerFragment:

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    public DatePickerFragment() {
        // Required empty public constructor
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        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);
        DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), this, year, month, day);

        // Create a new instance of DatePickerDialog and return it
        return datePickerDialog;
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        TimePickerFragment newFragment = new TimePickerFragment();
        newFragment.show(getActivity().getFragmentManager(), "timePicker");
    }
}

在DatePickerFragment中,我也在DatePickerFragment关闭后调用TimePickerFragment。

我的问题是:如何在ChildViewHolder中获取日期信息(也包括时间信息)?

1 个答案:

答案 0 :(得分:0)

解决方案是在DatePikerFragment中创建自己的侦听器:

span.vertical-text

在ChildViewHolder中设置此侦听器:

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{

    private DateDialogFragmentListener dateDialogFragmentListener;

    public void setDateDialogFragmentListener(DateDialogFragmentListener dateDialogFragmentListener) {
        this.dateDialogFragmentListener = dateDialogFragmentListener;
    }

    public interface DateDialogFragmentListener{
        void onDateSelected(int year, int month, int dayOfMonth);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        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);
        DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), this, year, month, day);

        return datePickerDialog;
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        dateDialogFragmentListener.onDateSelected(year, month, dayOfMonth);
        TimePickerFragment newFragment = new TimePickerFragment();
        newFragment.show(getActivity().getFragmentManager(), "df");
    }
}