简体   繁体   中英

CalendarView returns always current day ignoring what user selects

There is a CalendarView in screen and I want to display the selected date on the same screen. I want it to change dynamically as user changes the selected date.

This works perfect in my phone LG G2. Whenever I select a date in calendar, it shows that date. But in Nexus 10 tablet whatever date I select, it returns the current day. And if I keep changing the date, nothing changes it always display the current day.

Here is the relevant part of my code.

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootview = inflater.inflate(R.layout.calendar_layout, container, false);
    calendarView = (CalendarView) rootview.findViewById(R.id.calendarView);
    selectedDate = (TextView) rootview.findViewById(R.id.textView5);
    calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener(){
        public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth){
            date = new Date(calendarView.getDate());
            dateString = simpleDate.format(date);
            MainActivity.selectedDate=dateString;
            selectedDate.setText(dateString);
        }
    });
    return rootview;
}

Is there anything wrong with the code? What may be reason that CalendarView acts different in phone and tablet? Thank you for your answers.

I had the exact same problem on devices with Lollipop. The only thing I could think of was working with a Calendar within the onSelectedDayChange

public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth){
    Calendar c = Calendar.getInstance();
    c.set(year, month, dayOfMonth);
    Date d = c.getTime; // here is your correct date
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM