How do I store Object that have other Objects as Instance Variable in an android SQLite database.
Basically, I have my JSON response from Rest Api call to look like:
{
"announcements": [
{
"courseId": "196014605914",
"id": "269865109791",
"text": "Ignore the previous link. Join the already started CHM 112",
"state": "PUBLISHED",
"alternateLink": "https://classroom.google.com/c/MTk2MDE0NjA1OTE0/p/MjY5ODY1MTA5Nzkx",
"creationTime": "2021-02-08T09:18:05.420Z",
"updateTime": "2021-02-08T09:18:05.415Z",
"creatorUserId": "101562079220031604157"
},
{
"courseId": "196014605914",
"id": "246419265642",
"text": "Note2",
"materials": [
{
"driveFile": {
"driveFile": {
"id": "1CjL0RV7PdcIrrRWii1ApUown3YoyEKTx",
"title": "HYDROCARBONS2-C-C, C=C AND ALKYNES.pdf",
"alternateLink": "https://drive.google.com/open?id=1CjL0RV7PdcIrrRWii1ApUown3YoyEKTx",
"thumbnailUrl": "https://drive.google.com/thumbnail?id=1CjL0RV7PdcIrrRWii1ApUown3YoyEKTx&sz=s200"
},
"shareMode": "VIEW"
}
}
],
"state": "PUBLISHED",
"alternateLink": "https://classroom.google.com/c/MTk2MDE0NjA1OTE0/p/MjQ2NDE5MjY1NjQy",
"creationTime": "2021-01-25T10:41:45.094Z",
"updateTime": "2021-01-25T10:41:44.260Z",
"creatorUserId": "101562079220031604157"
},
],
"nextPageToken": "GkUSQxJBCLHA5Pr4LhI4Cg5iDAi2pfD_BRDAoOKzAQoOYgwItqXw_wUQgMCNtwEKCgiAgICg29jhsFoKCgiAgIDgwu223Hk"
}
This is part of the Announcement Object but some have others don't have a Material object as seen above. The problem is not all Announcements have Material object and more so a single Announcement object can have upto 20 material object.
My data model looks like below:
public class CourseDetailsItem{
private final String message;
private final Long time;
private final String author;
private final List<Material> materials;
//... Appropriate getters and setters
//Material.java
public class Material{
private final String label;
private final String link;
//... Appropriate getters and setters
What is the best way to store the Object for offline usage?
In my DbHelper class I'm confused
SQLiteDatabase db = this.getWritableDatabase();
String CREATE_COURSE_TABLE = "CREATE TABLE IF NOT EXISTS _"+courseId+" ("+ ID +" INTEGER PRIMARY KEY," + MESSAGE + " TEXT,"+ TIME + " INTEGER," + AUTHOR + " TEXT," + LABEL + " TEXT," + LINK + " TEXT"+")";
db.execSQL(CREATE_COURSE_TABLE);
ContentValues values = new ContentValues();
values.put(MESSAGE, courseDetailsItem.getMessage());
values.put(TIME, courseDetailsItem.getTime());
values.put(AUTHOR, courseDetailsItem.getAuthor());
//values.put(); HERE HOW DO I STORE MATERIAL OBJECT
//Considering the fact that Material Object could be upto Twenty for each of my entry
Anyone please...
And please no negative energy here, I just couldn't get my brain about a solution in here