3

I've got a fragment which calls another fragment (DialogFragment). I know that it is not best practice and that's why I am asking.

In fragment 1 I have got an EditText which provides a date picker dialog onClick as a fragment.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = (LinearLayout)inflater.inflate(R.layout.tab_frag1_layout, container, false);

    EditText dateInput  = (EditText) view.findViewById(R.id.editTextGeb);
    dateInput.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showDatePicker();
        }
    });
    return view;
}

private void showDatePicker() {
    DatePickerFragment date = new DatePickerFragment();

    // set date of today
    Calendar calender = Calendar.getInstance();
    Bundle args = new Bundle();
    args.putInt("year", calender.get(Calendar.YEAR));
    args.putInt("month", calender.get(Calendar.MONTH));
    args.putInt("day", calender.get(Calendar.DAY_OF_MONTH));
    date.setArguments(args);

    date.setCallBack(ondate);
    date.show(getChildFragmentManager(), "Date Picker");
}

OnDateSetListener ondate = new OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        Log.i(TAG, text);
        // here is the place to find the right element
    }
};

And here is the DatePickerFragment:

public class DatePickerFragment extends DialogFragment {
private static final String TAG = "DatePickerFragment";
OnDateSetListener ondateSet;

private int year; 
private int month;
private int day;

public DatePickerFragment() {}

public void setCallBack(OnDateSetListener ondate) {
    ondateSet = ondate;
}

@Override
public void setArguments(Bundle args) {
    super.setArguments(args);
    year = args.getInt("year");
    month = args.getInt("month");
    day = args.getInt("day");
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    return new DatePickerDialog(getActivity(), ondateSet, year, month, day);
}

My question is: How do I get the date out of my DatePickerFragment into my EditText element? I cannot find the right EditText element by requesting it over view.findViewById(R.id.editTextGeb); since I am not in correct view. Do I need to communicate over the hosting activity? And if so is there a good example somewhere?

I found the answer.

What I actually have is a fragment which gets dynamically attached to a FrameLayout (I use tabs in my project). Thus I wasn't able to find it by ID and had to call its parent element! There is a good explaination of what helped me understanding the problem here: Get Fragment dynamically attached to <FrameLayout>?

I use the DatePickerFragment provided by Google: http://developer.android.com/guide/topics/ui/controls/pickers.html

In the host activity class I implement the DatePickerDialog.OnDateSetListener and override its onDataSet() method. Inside that method I call the PARENT element of my fragment to get access to my fragment.

@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
    Tab1Fragment tab1 = (Tab1Fragment) getSupportFragmentManager().findFragmentById(R.id.realtabcontent);
    EditText et = (EditText) tab1.getView().getRootView().findViewById(R.id.editTextGeb);
    et.setText(day + "." + month + "." + year);
}

That's it. Thanks for helping. Direct fragment to fragment communication chould be avoided according to the documention.

Community
  • 1
  • 1
aszel
  • 73
  • 2
  • 10
  • After a simple Google Search, I found http://developer.android.com/guide/topics/ui/controls/pickers.html and http://stackoverflow.com/questions/11754663/android-datepicker-fragment-how-to-do-something-when-the-user-sets-a-date . What is wrong with either of these? – BLaZuRE Jul 25 '13 at 07:59
  • Thanks for your answer. There is nothing wrong with these. Your answer made me rethink my whole code and I deleted it. The problem was deeper and I try to explain what happened further below. – aszel Jul 25 '13 at 13:45

1 Answers1

1

You should add your fragment #1 to your DatePickerFragment as Target. This way:

DatePickerFragment datePicker = new DatePickerFragment();
datePicker.setTargetFragment(WhateverClassFragment1Is.this, 0);

And then in your DatePickerDialog code:

OnDateSetListener ondate = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    Log.i(TAG, text);
    WhateverClassFragment1Is target = (WhateverClassFragment1Is) getTargetFragment();
    target.yourHandyMethod(dayOfMonth, monthOfYear, year);
}

};

Hope that helps!

Adrián Rodríguez
  • 1,836
  • 15
  • 16