In my activity I have two TextView (startDatePicker & endDatePicker) and when they are clicked a datePicker dialog should show up. Until here everything works fine, but I need, after the user choose the date, to set the text of the corresponding clicked textView with the user's choice. I found another post like this and I took the cue from it : https://stackoverflow.com/a/24534147/10113273
This is my DatePickerDialogFragment Class:
public class DatePickerDialogFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
public static final int START_DATE = 0;
public static final int END_DATE = 1;
private int current = 0;
private int chosenDate;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
Bundle bundle = this.getArguments();
if (bundle != null) {
chosenDate = bundle.getInt("DATE", 0);
}
switch (chosenDate) {
case START_DATE:
current = START_DATE;
return new DatePickerDialog(getActivity(), this, year, month, dayOfMonth);
case END_DATE:
current = END_DATE;
return new DatePickerDialog(getActivity(), this, year, month, dayOfMonth);
}
return null;
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
if (current == START_DATE) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String dateString = DateFormat.getDateInstance().format(calendar.getTime());
//Set the startDate textView text with the dateString
} else {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String dateString = DateFormat.getDateInstance().format(calendar.getTime());
//Set the endDate textView text with the dateString
}
}
}
Inside the onDateSet how can I access the textView? I probably think that the answer is that it cannot be done. If I am right, what could I do to solve this?
Here where I set the onClickListener on the textview in the Activity:
startDatePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putInt("DATE",0);
DialogFragment dialogFragment = new DatePickerDialogFragment();
dialogFragment.setArguments(bundle);
dialogFragment.show(getSupportFragmentManager(),"date picker");
}
});
endDatePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putInt("DATE",1);
DialogFragment dialogFragment = new DatePickerDialogFragment();
dialogFragment.setArguments(bundle);
dialogFragment.show(getSupportFragmentManager(),"date picker");
//endDatePicker.setText(endDate);
}
});