【发布时间】:2021-05-17 04:18:30
【问题描述】:
如何将具有其他对象作为实例变量的对象存储在 android SQLite 数据库中。
基本上,我从 Rest Api 调用得到的 JSON 响应看起来像:
{
"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"
}
这是 Announcement 对象的一部分,但有些没有如上所示的 Material 对象。问题不是所有的 Announcement 都有 Material 对象,而且一个 Announcement 对象最多可以有 20 个 Material 对象。
我的数据模型如下所示:
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
存储对象以供离线使用的最佳方式是什么?
在我的 DbHelper 类中我很困惑
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
请大家...
请不要在这里有负能量,我只是无法在这里找到解决方案
【问题讨论】:
标签: android sqlite pojo jsonschema2pojo