1

I have a MainFragment from where a button creates an AlertDialog in there there are multiple fields and one of them is the date, in the date button I call an DatePickerDialog and select the date, so my question is :
How can I retrieve that date and set it on the TextView of the alert dialog on theMainFragment?

This is on the AlertDialog on the main fragment:

public class FragmentInicio extends Fragment{
   String date;

  public void addDate(){

  View view = (LayoutInflater.from(getActivity())).inflate(R.layout.alert_dialog,null);

AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
alertDialog.setView(view);

final EditText date_EditText = view.findViewById(R.id.dateEditText);
final ImageButton dateButton= view.findViewById(R.id.tareaFechaButton);
dateButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        CalendarPickerFragment newFragment = new CalendarPickerFragment();
        newFragment.show(getFragmentManager(), "DatePicker");

    }
});

date_EditText.setText(date);

alertDialog.setTitle("New Date");
alertDialog.setCancelable(true).setPositiveButton("Save", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
       //Some stuff
    }
});

alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
    }
});
Dialog dialog = alertDialog.create();
dialog.show();

}

public static void displaydate(int year, int monthOfYear, int dayOfMonth) {
    date = String.format("%02d", dayOfMonth) + "/" + String.format("%02d", monthOfYear + 1) + "/" +
            String.format("%02d", year);
    Log.d("DTAG", "date: "+date);
}

}

This is on the CalendarPickerFragment

Update

public class CalendarPickerFragment extends DialogFragment implements  DatePickerDialog.OnDateSetListener {

    private CalendarPickerFragment.OnDateReceiveCallBack mListener;
    private Context context;

    public interface OnDateReceiveCallBack {
         void onDateReceive(int dd ,int mm, int yy);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;

        try {
            mListener = (CalendarPickerFragment.OnDateReceiveCallBack) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString() + " must implement OnDateSetListener");
        }
    }

    public CalendarPickerFragment(){
    }



    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        final Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DAY_OF_MONTH);

        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    @Override
    public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
        Log.d(TAG, i + "/" + "/" + i1 + "/" + i2);
        mListener.onDateReceive(i,i1,i2);
    }
}

MainActivity

public class MainActivty implements CalendarPickerFragment.OnDateReceiveCallBack{
@Override
    public void onDateReceive(int dd, int mm, int yy) {
        MainFragment.displaydate(dd,mm,yy);
    }
}
  • https://stackoverflow.com/questions/50119457/how-to-change-the-text-of-a-running-alertdialog two threads for same quest.. i answered you there https://stackoverflow.com/questions/50119457/how-to-change-the-text-of-a-running-alertdialog – Adeel Turk May 01 '18 at 17:39

1 Answers1

2

You can do it either the ugly way getting your parent Activity from your Fragment using this method and then using setters to fill attributes from your parent Activity, or either using interfaces as shown here.

EDIT

Check this question.

Community
  • 1
  • 1
Nark
  • 454
  • 1
  • 7
  • 18
  • The problem is I need the date to be set on the _AlertDialog_ that was open, if I pass the string is just going to add the text to the next time I open the _AlertDialog_ – Alfonso Briceño Apr 30 '18 at 23:04
  • @AlfonsoBriceño Can't you use `setText()` method from `EditText` to modify it whenever you want to? – Nark May 01 '18 at 11:36
  • I used this method [link](https://stackoverflow.com/questions/47740237/add-date-picker-fragment-inside-fragment) to get the string from the CalendarPicker to the main fragment, but this stores the date on the string after creating the `EditText` on the `AlertDialog` so the `EditText` gets the string that was set before the creation of the `AlertDialog`. Its a silly problem but I am not that good at Java – Alfonso Briceño May 01 '18 at 14:30
  • Any way to do it will be highly appreciated! – Alfonso Briceño May 01 '18 at 16:39
  • @AlfonsoBriceño have you tried to use `setText()` function as I mentioned before? – Nark May 01 '18 at 16:58
  • Yes I did, but the setText is runned when I create the Dialog, therefore it puts the date String before I select the date, and is not updated when I select the date. Do you get my dilemma? – Alfonso Briceño May 01 '18 at 17:03
  • @AlfonsoBriceño I don't get it : you're displaying to the user something so that he can pick a date, and at the same time you're diplaying him a Dialog box? – Nark May 01 '18 at 17:06
  • Yes I am displaying him a AlertDialog where there are multiple fields in one of them there is a button and a Edit Text, when he presses the button a CalendarPicker shows up. I would like to set the selected date on the Edit Text – Alfonso Briceño May 01 '18 at 17:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/170160/discussion-between-nofix-and-alfonso-briceno). – Nark May 01 '18 at 17:13