Pass CalendarView date from activity to fragment

806 views Asked by At

Currently I am having trouble passing the date I've selected from the CalendarView in an Activity to pass to a Fragment to display the selected date in TextView. So this is my codes in the Activity, where the CalendarView is

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_planner);

    cvSelectPlannerDate = (CalendarView) findViewById(R.id.cvSelectPlannerDate);

    fragmentPlannerDayView = new FragmentPlannerDayView();

    // get current date
    Calendar date = Calendar.getInstance();
    // for date format
    SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");
    // set string to format current date
    String curDate = sdf.format(date.getTime());
    // print date in log cat
    Log.d("CUR_DATE", curDate);

    // get date changed
    cvSelectPlannerDate.setOnDateChangeListener(new OnDateChangeListener() {
        public void onSelectedDayChange(CalendarView view, int year,
                int month, int day) {
            // add one because month starts at 0
            month = month + 1;
            // output to logcat
            String newDate = year + "-" + month + "-" + day;
            Log.d("NEW_DATE", newDate);

            send(day, month, year);
        }
    });

}

public void send(int day, int month, int year) {
    Bundle date = new Bundle();
    date.putInt("Day", day);
    date.putInt("Month", month);
    date.putInt("Year", year);

    FragmentPlannerDayView sendToFragment = new FragmentPlannerDayView();
    sendToFragment.setArguments(date);

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTrans = fragmentManager.beginTransaction();
    fragmentTrans.replace(R.id.content_frame, sendToFragment);
    fragmentTrans.addToBackStack(FragmentPlannerDayView.class.getName());
    fragmentTrans.commit();
}

I've managed to display current date and new date in logcat - I got this part from another answer: Android CalendarView: How do I get the date in correct format?

The next part of code is in my Fragment class,

public class FragmentPlannerDayView extends Fragment {

    Context context;
    public FragmentPlannerDayView() {

    }

    Calendar calendar;
    TextView tvPlannerDate;

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

        context = rootView.getContext();

        tvPlannerDate = (TextView) rootView.findViewById (R.id.tvPlannerDate); 

        Bundle bundle = this.getArguments();

        int day= bundle.getInt("Day");
        int month = bundle.getInt("Month");
        int year = bundle.getInt("Year");

        String date = day + "-" + month + "-" + year;
        tvPlannerDate.setText(date);

        return rootView;
    }

}

What I'm trying to do is to retrieve my date from the bundle in the previous activity. However, when I try to run these codes, all that happens is that my app crashes (Unfortunately, your app has stopped working). I can't find out what is the problem in the application. All I want is for the user to select a date in the CalendarView, and upon selecting, they will be redirected to the fragment in question whereby the selected date will be displayed in a TextView.

By the way, here's the logcat message enter image description here

0

There are 0 answers