I've already seen a threads similar to this, but none of them answers my question.
So let's say we have two classes:
- Lesson
- Break
Both of them are implementing SchoolEntity interface, which has the following methods:
- Date getStartDate()
- Date getEndDate()
Break class does nothing beside implementing this interface, but Lesson has additional fields and getters, like:
- subject
- room
- teacher, etc.
Now I want to save instance of SchoolEntity using Android's Room Persistence Library.
This is the List I want to be saved:
List<SchoolEntity> timetable:
1. Lesson@13da43:
- Date startDate = Date@ha94j8
- Date endDate = Date@9kf8kf
- String subject = "math"
- String room = "124"
- String teacher = "Joseph Fold"
2. Break@j7gfsd:
- Date startDate = Date@738291
- Date endDate = Date@fj8fjn
And I want it to be saved in SQL Database like this:
+----------------------+---------------------+--------+---------+------+-------------+
| start_date | end_date | type | subject | room | teacher |
+----------------------+---------------------+--------+---------+------+-------------+
| 2019.01.12 08:00:00 | 2019.01.12 08:45:00 | LESSON | math | 124 | Joseph Fold |
| 2019.01.12 08:45:00 | 2019.01.12 08:55:00 | BREAK | NULL | NULL | NULL |
+----------------------+---------------------+--------+---------+------+-------------+
Note: start_date is primary key
And finally I want to be able to retrive saved information as:
LiveData<List<SchoolEntity>> timetable
My question is:
Am I able to do this using Android's Room Persistence Library? If so, how to achieve it? If no what should I use instead?
My language is Java, but Kotlin will also make me.