1

I'm new in android programming and I've sometimes some problems. I have to pass day, month and year selected by DatePicker Dialog fragment to the Activity that invoke the fragment. I've used use the android guide code:

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
    }
}

I've already asked in another forum and they suggest me to override show() method to pass another variable like this:

@Override
public void show (FragmentManager manager, String tag, DatePickerDialog.OnDateSetListener dateSetListener) {
  super(manager, tag);
  this.dateSetListener = dateSetListener;
}

No way, it doesn't work. How can I do?

Sorry for my bad english and for the stupid question :( Thanks in advance, Damiano

EDIT:

I have been reviewed the code by implementing interface, but I still have problems:

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

What is the problem? :(

Damien
  • 921
  • 4
  • 13
  • 31
  • did you read this? http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity – tyczj Jan 26 '15 at 18:28
  • Did you searched?
    http://stackoverflow.com/questions/13445594/data-sharing-between-fragments-and-activity-in-android
    http://stackoverflow.com/questions/21633887/getting-variable-reference-from-fragment-to-activity
    http://stackoverflow.com/questions/9343241/passing-data-between-a-fragment-and-its-container-activity
    http://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android
    – Cristian Olaru Jan 26 '15 at 20:39
  • thanks a lot, I've just edited the post – Damien Jan 26 '15 at 21:01

0 Answers0