Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CalendarView getDate method returns current date, not selected date... What am I doing wrong?

My calendarView fails to return the selected date, instead returning some default that always points to today.

I am of course changing the date selected in my calendar, and it indeed displays as having changed. I tried inspecting the view in debug mode, but didn't find anything.

I am running this in a simulator, not on a real phone... Should I modify some settings? Am I missing something important? Because it really is confusing that I'm not getting the selected date, but the current one.

<CalendarView
                            android:id="@+id/view_calendar_create_event_date"
                            android:layout_width="wrap_content"
                            android:layout_height="0dp"
                            android:layout_weight="1" />

This is called from the event listener

protected void createEvent(View view){
        TextView eventNameView = (TextView) this.findViewById(R.id.createEventNameInput);
        String eventName = eventNameView.getEditableText().toString();

        CalendarView eventOccursOnView = (CalendarView) this.findViewById(R.id.view_calendar_create_event_date);
        long eventOccursOn = eventOccursOnView.getDate();
        Date temporary = new Date(eventOccursOn);

        Event newEvent = new Event(eventName, "", 0, 0, eventOccursOn);
        newEvent.save(view.getContext());
    }

and this is how I'm setting my event listener

saveButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                // Create the event
                EventDetailsActivity.this.createEvent(view);

                // Notify the user
                Snackbar.make(view, "Successfully created a new event!", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();

                // Return to the previous activity
                finish();
            }
        });
like image 858
vlad-ardelean Avatar asked Sep 10 '17 18:09

vlad-ardelean


1 Answers

You need to implement setOnDateChangeListener

long eventOccursOn;

eventOccursOnView.setOnDateChangeListener(new OnDateChangeListener() {
    //show the selected date as a toast
    @Override
    public void onSelectedDayChange(CalendarView view, int year, int month, int day) {
        Toast.makeText(getApplicationContext(), day + "/" + month + "/" + year, Toast.LENGTH_LONG).show();
        Calendar c = Calendar.getInstance();
        c.set(year, month, day);
        eventOccursOn = c.getTimeInMillis(); //this is what you want to use later
    }
 });
like image 97
Mithun Sarker Shuvro Avatar answered Sep 23 '22 18:09

Mithun Sarker Shuvro