Android 将变量从片段传递到活动(日期选择器)

Android 将变量从片段传递到活动(日期选择器),android,android-fragments,datepicker,Android,Android Fragments,Datepicker,我是android编程新手,有时会遇到一些问题。我必须将DatePicker对话框片段选择的日期、月份和年份传递给调用该片段的活动。我使用了android指南代码: public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { @Override public Dia

我是android编程新手,有时会遇到一些问题。我必须将DatePicker对话框片段选择的日期、月份和年份传递给调用该片段的活动。我使用了android指南代码:

public static class DatePickerFragment extends DialogFragment
                            implements DatePickerDialog.OnDateSetListener {

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

    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
    }
}
我已经在另一个论坛上问过了,他们建议我重写show方法来传递另一个变量,如下所示:

@Override
public void show (FragmentManager manager, String tag, DatePickerDialog.OnDateSetListener dateSetListener) {
  super(manager, tag);
  this.dateSetListener = dateSetListener;
}
不可能,它不起作用。我该怎么办

对不起,我的英语不好,还有一个愚蠢的问题: 提前感谢,, 达米亚诺

编辑:

我已经通过实现接口检查了代码,但我仍然存在问题:

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
    onDataSceltaListener mListener;
    @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);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        mListener.onDataScelta(year,month,day);

    }

    public interface OnDataSceltaListener {
        public void onDataScelta(int year, int month, int day);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnDataSceltaListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
        }
    }
}

有什么问题吗

你读过这个吗?你找过了吗?>>非常感谢,我刚刚编辑了这篇文章