I am trying to display different date in different TextViews in a fragment following the answer of MH to this question at TimePicker onTimeSet not being called but I am not getting the same results for a fragment.
I have added a separate DatePickerFragment class in which I am using an Interface class which is used as a callback method and is defined in a fragment class Medication.
How do I set multiple date pickers in a fragment?
The DatePickerFragment is written as:
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
private int mId;
DatePickerDialogListener mListener;
protected static DatePickerFragment newInstance(int id) {
Bundle args = new Bundle();
args.putInt("picker_id", id);
DatePickerFragment fragment = new DatePickerFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// ... omitted
final Calendar c = Calendar.getInstance();
int mDay = c.get(Calendar.DAY_OF_MONTH);
int mMonth = c.get(Calendar.MONTH);
int mYear = c.get(Calendar.YEAR);
mId = getArguments().getInt("picker_id");
mListener = getActivity() instanceof DatePickerFragment.DatePickerDialogListener ? (DatePickerFragment.DatePickerDialogListener) getActivity() : null;
// Create a new instance of TimePickerDialog and return it
return new DatePickerDialog(getActivity(), this, mYear, mMonth, mDay);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
if (mListener != null) mListener.onDateSet(mId, view, year, month, dayOfMonth);
}
public static interface DatePickerDialogListener {
public void onDateSet(int id, DatePicker view, int year, int month, int dayOfMonth);
}
}
The Fragment class Medication which would use this datepickerdialog has the following code:
public class Medication extends Fragment implements DatePickerFragment.DatePickerDialogListener {
private static final int FIRST_VISIT_ID = 1;
private static final int SECOND_VISIT_ID = 2;
private static final int THIRD_VISIT_ID = 3;
private int mYear;
private int mMonth;
private int mDay;
protected static TextView date1;
protected static TextView date2;
protected static TextView date3;
public Medication() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_medication, container, false);
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
date1 =(TextView) view.findViewById(R.id.visit_date1);
date2 =(TextView) view.findViewById(R.id.visit_date2);
date3 =(TextView) view.findViewById(R.id.visit_date3);
date1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = DatePickerFragment.newInstance(FIRST_VISIT_ID);
newFragment.show(getFragmentManager(), "timePicker");
}
});
date2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = DatePickerFragment.newInstance(SECOND_VISIT_ID);
newFragment.show(getFragmentManager(), "timePicker");
}
});
date3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = DatePickerFragment.newInstance(THIRD_VISIT_ID);
newFragment.show(getFragmentManager(), "timePicker");
}
});
date1.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mDay).append("/")
.append(mMonth + 1).append("/")
.append(mYear).append(" "));
date2.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mDay).append("/")
.append(mMonth + 1).append("/")
.append(mYear).append(" "));
date3.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mDay).append("/")
.append(mMonth + 1).append("/")
.append(mYear).append(" "));
return view;
}
@Override
public void onDateSet(int id, DatePicker view, int year, int month, int dayOfMonth) {
Log.i("DatePicker", "Date picker set from id " + id + "!");
if(id == FIRST_VISIT_ID)
{
this.date1.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(dayOfMonth).append("/")
.append(month + 1).append("/")
.append(year).append(" "));
}
if(id == SECOND_VISIT_ID)
{
this.date2.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(dayOfMonth).append("/")
.append(month + 1).append("/")
.append(year).append(" "));
}
if(id == THIRD_VISIT_ID)
{
this.date3.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(dayOfMonth).append("/")
.append(month + 1).append("/")
.append(year).append(" "));
}
}
}
I have tried this method of adding multiple pickers for Time Picker Dialog as well but the same problem occurs that onTimeSet method in Medication Class is not being called as in here onDateSet.
The problem is that the dialog opens when I click on a textView but after I change the date, the date of that textView doesn't change. Also the log info message is not shown as the onDateSet method is not being called. Maybe the mListener is null in this case, I can only guess. Please help me solve this problem. Thanks in advance.