반응형
- ViewModel을 사용하여 데이터를 저장하기
- LiveData와 Factory 패턴으로 실시간 데이터 변경
1. MyViewModel.java
public class MyViewModel extends ViewModel {
private MutableLiveData<Reserves> reserves;
public MutableLiveData<Reserves> getReserves() {
if (reserves == null) {
reserves = new MutableLiveData<Reserves>();
}
return reserves;
}
}
2. MyViewModelFactory.java
public class MyViewModelFactory implements ViewModelProvider.Factory {
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
try {
return modelClass.newInstance();
} catch (IllegalAccessException e) {
e.printStackTrace();
throw new RuntimeException("팩토리 런타임 에러");
} catch (InstantiationException e) {
e.printStackTrace();
throw new RuntimeException("팩토리 런타임 에러2");
}
}
}
Reserves.java
3. Reserves.java
public class Reserves {
private static Integer nowYear;
private static Integer nowMonth;
private static Integer nowDay;
private static Integer nowHour;
private static Integer nowMinute;
private static Boolean timePicker = false;
private static Boolean calendarPicker = false;
public Reserves(Integer nowYear, Integer nowMonth, Integer nowDay){
this.calendarPicker = true;
this.nowYear = nowYear;
this.nowMonth = nowMonth;
this.nowDay = nowDay;
}
public Reserves(Integer nowHour, Integer nowMinute) {
this.timePicker = true;
this.nowHour = nowHour;
this.nowMinute = nowMinute;
}
public void setNowHour(Integer hour){nowHour = hour;}
public Integer getNowHour(){return nowHour;}
public void setNowMinute(Integer minute){nowMinute = minute;}
public Integer getNowMinute(){return nowMinute;}
public void setNowYear(Integer year){nowYear = year;}
public Integer getNowYear(){return nowYear;}
public void setNowMonth(Integer month){nowMonth= month;}
public Integer getNowMonth(){return nowMonth;}
public void setNowDay(Integer day){nowDay = day;}
public Integer getNowDay(){return nowDay;}
public Boolean getCalendarPicker(){return calendarPicker;}
public Boolean getTimePicker(){return timePicker;}
}
4. MainActivity.java
reserves = new Reserves(changeYear, changeMonth, changeDay);
//뷰모델 객체 생성
viewModel = new ViewModelProvider(this, new MyViewModelFactory())
.get(MyViewModel.class);
//옵저버 정의 - 데이터가 변하는 이벤트 발생시 처리할 핸들러(람다)
Observer<Reserves> PickerObserver = PickerData -> reserves = PickerData;
//뷰모델에 옵저버 등록
viewModel.getReserves().observe(this, PickerObserver);
viewModel.getReserves().setValue(reserves);
ex) 데이터의 사용
viewModel.getReserves().getValue().getNowYear()
viewModel.getReserves().getValue().getNowMonth()
viewModel.getReserves().getValue().getNowDay()
Reference
https://developer.android.com/topic/libraries/architecture/viewmodel?hl=ko#java
https://javachoi.tistory.com/138
https://readystory.tistory.com/173
반응형
'Android > Android Java' 카테고리의 다른 글
[Android Java] configChanges 설정 (1) | 2023.10.27 |
---|---|
[Android Java] 날짜와 시간 (1) | 2023.10.27 |
[Android Java] CalendarPicker, TimePicker (Viewmodel, LiveData, Factory, EventBus) (2) | 2023.10.27 |
[Android Java] Room DB 사용하기 (0) | 2023.10.27 |
[Android Java] System.arraycopy (0) | 2023.10.27 |