2017-03-03 18 views
0

사용자가 날짜를 선택하고 활동을 입력 할 수있는 CalendarView가 있습니다.Java Android CalendarView selectedDateChanged가 업데이트되지 않음

I는 선택된 날짜가 변경 될 때마다 다음과 같이 호출되는 메소드를 가지고

private void selectedDateChanged(CalendarView view, int year, int month, int dayOfMonth){ 
     //note: _Calendar is set to the view when activity loads hence the 
     //the reason for not using view.getDate(); 
     Long timeSinceEpoch = _Calendar.getDate(); 

     GregorianCalendar calendar = new GregorianCalendar(); 
     calendar.setTimeInMillis(timeSinceEpoch); 

     System.out.println(String.format("With gregorian calendar \n Day: %s \n Month: %s \n Year: %s", 
       calendar.DAY_OF_MONTH, calendar.MONTH, calendar.YEAR)); 

    } 

마다 날짜 선택한 변경 호출되는 상기 방법은, 문제가되는 GregorianCalendar 메소드마다 갱신되지이고 호출됩니다. 새 날을 선택할 때마다

I/System.out: With gregorian calendar 
I/System.out: Day: 5 
I/System.out: Month: 2 
I/System.out: Year: 1 

이 인쇄되고 새 날짜가 선택되면 업데이트되지 않습니다.

나는 업데이트 할 GregorianCalendar를 강제하는 방법을 알아낼 수 없습니다 및 CalendarView를위한 Javadoc은 Calendar.DAY_OF_MONTH을 밝혀으로 GETDATE()가 유닉스 시대

+0

전달 된 매개 변수'year','month','dayOfMonth'를 사용하지 않는 이유는 무엇입니까? – petey

+1

@petey 이러한 매개 변수가없는 다른 메서드에서 날짜를 가져올 필요가 있습니다. 이것은 테스트하기에 편리한 장소 일뿐입니다. – kalenpw

답변

0

부터 밀리 초 단위로 현재 선택된 날짜를 반환해야 함을 말한다

: Calendar.MONTHCALENDAR.YEAR 당신이 그래서 내가 적절한 코드는, 사실을 알게되었다 오히려 간단 고정 Calendar.get();

를 호출 할 때를 찾고 있습니다하지 그 정보를 필드 만 나타내는 정수입니다

private void selectedDateChanged(CalendarView view, int year, int month, int dayOfMonth){ 
     //note: _Calendar is set to the view when activity loads hence the 
     //the reason for not using view.getDate(); 
     Long timeSinceEpoch = _Calendar.getDate(); 

     GregorianCalendar calendar = new GregorianCalendar(); 
     calendar.setTimeInMillis(timeSinceEpoch); 

     //Notice instead of calling cal.DAY_OF_MONTH directly 
     //I now call calendar.get(Calendar.DAY_OF_MONTH) 
     System.out.println(String.format("With gregorian calendar \n Day: %s \n Month: %s \n Year: %s", 
       calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.MONTH), calendar.get(Calendar.YEAR))); 

    } 
0
System.out.println(String.format("With gregorian calendar \n Day: %s \n Month: %s \n Year: %s", 
      dayOfMonth, month, year)); 
+0

캘린더 뷰에만 액세스 할 수있는 다른 방법으로 일, 월, 년을 가져올 수 있어야하기 때문에 의도적으로 매개 변수를 사용하지 않습니다. – kalenpw