hbk
hbk

Reputation: 1

My code is not working for Setting Date in Multiple TextViews

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.

Upvotes: 0

Views: 106

Answers (2)

hbk
hbk

Reputation: 1

I didn't get the answer to why above code doesn't work but after some search on net I found another very useful method for adding date or time in multiple TextView. Here I am adding two dates in fragment, same technique works for activity as well.

public class Medication extends Fragment implements View.OnClickListener{

    private DatePickerDialogFragment mDatePickerDialogFragment;

    protected static TextView date1;
    protected static TextView date2;

    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup     container,
    Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_medication, container, false);
    date1 = (TextView) view.findViewById(R.id.date1);
    date2 = (TextView) view.findViewById(R.id.date2);

    displayCurrentDate(); // display current date as default in date1 and date2

    mDatePickerDialogFragment = new DatePickerDialogFragment();

    date1.setOnClickListener(this);
    date2.setOnClickListener(this);

    }

    public void displayCurrentDate(){

    static Calendar calendar = Calendar.getInstance();
    private int year = calendar.get(Calendar.YEAR); // get current year
    private int month = calendar.get(Calendar.MONTH);// get current month
    private int day = calendar.get(Calendar.DAY_OF_MONTH); // get current day

    calendar.set(year, month, day);
    SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");

    // display Current Date in date1 and date2

    date1.setText(format.format(calendar.getTime()));
    date2.setText(format.format(calendar.getTime()));        

    }

    @Override
    public void onClick(View v) {

    int id = v.getId();

    if (id == R.id.date1) {
    mDatePickerDialogFragment.setDateFlag(FLAG_DATE_ONE);
    mDatePickerDialogFragment.show(getFragmentManager(), "datePicker1");
    }else if (id == R.id.date2) {
    mDatePickerDialogFragment.setDateFlag(FLAG_DATE_TWO);
    mDatePickerDialogFragment.show(getFragmentManager(), "datePicker2");
    }
    }

    public static class DatePickerDialogFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
    protected static final int FLAG_DATE_ONE = 1;
    protected static final int FLAG_DATE_TWO = 2;
    private int flag = 0;
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

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

    public void setDateFlag(int i) {
        flag = i;
    }

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, monthOfYear, dayOfMonth);
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
        if (flag == FLAG_DATE_ONE) {
            date1.setText(format.format(calendar.getTime()));
        } else if (flag == FLAG_DATE_TWO) {
            date2.setText(format.format(calendar.getTime()));
        }
    }
}

}

Note: Please add android:clickable = "true" for each TextView in xml layout for enabling the click action, otherwise the TextView won't be clickable.

I hope this will help others :)

Upvotes: 0

shtolik
shtolik

Reputation: 1368

Try to add some logs in onDateSet of a fragment to see what values are set there and if it calls a listener.

@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
    Log.i("Fragment/onDateSet", "Date picker set from id " + mId+ "!");
    if (mListener != null) 
        mListener.onDateSet(mId, view, year, month, dayOfMonth);
    else
        Log.w("Fragment/onDateSet", "But no listener available!");    
}

And try to put different tags for different fragments, because right now you are marking them with the same tag

    newFragment.show(getFragmentManager(), "timePicker2"); 

Upvotes: 0

Related Questions