I am working on an Android application where users can save meals and their amounts according to a specific date.
Time is stored as a yyyy-mm-dd
string in a SQLite Database at the moment (I probably will change the data type later on).
The user can select the date on a CalendarView
and the RecylerView
should just show the meals on the selected date. I don't want to retrieve all the rows but just query the rows depending on the date.
I am using the Room Persistence Library.
How can I achieve this?
I am using a LiveData<List<MYOBJECT>>
and the appropriated Observable Listener:
final MyViewModel myViewModel = ViewModelProviders.of(this).get(MyViewModel.class);
String myDate = getDateFromCalendarView();
myViewModel.getMealsOnDate(myDate).observe(this, new Observer<List<MYOBJECT>>() {
@Override
public void onChanged(@Nullable List<MYOBJECT> myObjects) {
adapter.setItems(myObjects); // adapter from RecyclerView
}
});
This is the query in my DAO interface:
@Query("SELECT * FROM meals WHERE meals_date = :date")
LiveData<List<MYOBJECT>> getMealsOnDate(String date);
At the moment the Observer does not react on a different value in myDate
therefore I can not query different rows from my database.
Is this even possible or do I have to stick with query the whole table and then select the data from my List<MYOBJECT
?