Android - Save the realmlist inside a realm object - android

I have a model
public class Response extends RealmObject {
#PrimaryKey
private String _id;
private RealmList<TaskResponse> task_responses;
private AssignmentRecord assignmentRecord;
private ResponseStatistic responseStats;
}
Now, i am trying to save task_responses realmlist inside the Response but couldn't succeed, below is the relevant code:
mRealm.beginTransaction();
response.setTaskResponses(taskResponses);
response.setAssignmentRecord(assignmentRecord);
response.setResponseStats(responseStatistic);
mRealm.commitTransaction();
When I placed debugger before this line
response.setTaskResponses(taskResponses);
the size of taskResponses was 5 but after execution it becomes empty and realm stores a empty list.
TaskResponse Model :
public class TaskResponse extends RealmObject {
#PrimaryKey
private String _id;
private String taskId;
private Boolean answered;
private String answer;
private TaskStatistic taskStat;
}

You can save like this it will work
Realm mRealm = Realm.getDefaultInstance();
mRealm.beginTransaction();
RealmList<TaskResponse> taskResponseslist = new RealmList();
taskResponseslist.addAll(taskResponses);
response.setTaskResponses(taskResponseslist);
response.setAssignmentRecord(assignmentRecord);
response.setResponseStats(responseStatistic);
mRealm.commitTransaction();
mRealm.close();

Related

Android ExpandableListview with Room database

I need to populate an ExpandableListView whose data is fetched from Room database.
There are answers on how to do this with SQLiteDatabase:
1) Android ExpandableListView and SQLite Database
2) Android ExpandableListView From Database
Is it possible to achieve the same with Room Database?
I have two tables: a) GroupHeader b) GroupContent
#Entity
public class GroupHeader implements Serializable {
#PrimaryKey(autoGenerate = true)
private int groupId;
private String groupName;
private String otherProperty1;
private String otherProperty2;
/* getters and setters */
}
#Entity
public class GroupContent implements Serializable {
#PrimaryKey(autoGenerate = true)
private int contentId;
private int groupId;
private String contentName;
private String otherProperty3;
private String otherProperty4;
/* getters and setters */
}
Any suggestions please?
Found the solution using #Relation.
Reference: https://developer.android.com/reference/android/arch/persistence/room/Relation
#Entity
public class GroupHeader implements Serializable {
#PrimaryKey(autoGenerate = true)
private int groupId;
private String groupName;
private String otherProperty1;
private String otherProperty2;
/* getters and setters */
}
#Entity
public class GroupContent implements Serializable {
#PrimaryKey(autoGenerate = true)
private int contentId;
private int groupId;
private String contentName;
private String otherProperty3;
private String otherProperty4;
/* getters and setters */
}
public class Wrapper {
#Embedded
private GroupHeader header;
#Relation(parentColumn="groupId", entityColumn="groupId", entity=GroupContent.class)
private List<GroupContent> contents;
/*getters & setters*/
}

how can get deletion or insertion notify realm inner list?

i have ticket class and message class like below.
i want set change listener just for inner message list in special ticket id no whole table message.
i read about result and object changeListener and Notifications but i can't find any solution ;
how can I do that ?
public class TicketPojo extends RealmObject {
private Date createAt;
private Date updateAt;
#PrimaryKey
private int id;
private String supporterName;
private int supporterID;
RealmList<MessagePojo> messages;
//....
}
and
public class MessagePojo extends RealmObject {
#PrimaryKey
int id;
String messageType;
String Message;
Date createAt;
Date updateAt;
//...
}

RealmDB custom fields

Can I define a field with unknown type in Realm model?
Sample classes :
public class Model1 extends RealmObject {
#PrimaryKey
private String _id;
private ? field1;
}
public class Model2 extends RealmObject {
#PrimaryKey
private String _id;
}
public class Model3 extends RealmObject {
#PrimaryKey
private String _id;
}
Now, the field1 in Model1 can be of type Model2 or Model3 which will be determined during run time. Is there any way I can achieve this?
No, you cannot do that. Dalinaum's comment is correct.
One way to achieve it is like;
public class Model1 extends RealmObject {
#PrimaryKey
private String _id;
private Model2 model2;
private Model3 model3;
}
public class Model2 extends RealmObject {
#PrimaryKey
private String _id;
}
public class Model3 extends RealmObject {
#PrimaryKey
private String _id;
}
and access it via;
if (model1.getModel2() == null) {
Model2 model = model1.getModel2()
// do something
} else {
Model3 model = model1.getModel3()
// do something
}

save One to Many Collections in ormlite for android

I'm new to android and ormlite and i have creating an database CRUD operation with One to Many Relationship. The problem is i can't save the Collection data from Parent Class to child collections data into database. Here is my code :
(Parent class)
#DatabaseTable
public class Schedule implements Serializable {
private static final long serialVersionUID = 1L;
#DatabaseField(generatedId = true)
public int id;
#DatabaseField
public Date date;
#DatabaseField
public String patient;
#DatabaseField
public String room;
#ForeignCollectionField(eager = false)
public Collection<Task> tasks = new ArrayList<Task>();
public Schedule() {
}
}
(Child Class)
#DatabaseTable
public class Task implements Serializable {
private static final long serialVersionUID = 1L;
#DatabaseField(generatedId = true)
public int id;
#DatabaseField
public Date time;
#DatabaseField
public boolean done;
#DatabaseField
public String note;
#DatabaseField(foreign=true,foreignAutoRefresh=true)
public Schedule schedule = new Schedule();
public Task() {
}
}
When i save the data (Schedule class) with collection (tasks), it don't save the collection. Am i missing something ?
Thanks.
When i save the data (Schedule class) with collection (tasks), it don't save the collection. Am i missing something?
So there is no easy way for ORMLite to know that your collection of tasks has not already been inserted into the database. If you need to create them they you should either do that yourself or initialize the tasks field with:
parentDao.assignEmptyForeignCollection(parent, "tasks");
This will turn your tasks field into a foreign collection so any add(...) calls to it will be added also to the database.

How can I "map" in ORMLite a base object that all Database entities extend

I have an object that I call BaseObject which all classes that are persisted extend. This class has an _id field, dateCreated, dateModified, etc.... The database tables that correspond to all objects that extends this base object obviously have columns that store these values.
How can I set up these fields using annotations so all these fields are persisted to the database? Can I do this or will I need to add these fields to the POJOs directly? I have included below my base object and one of the classes that extends it.
public class BaseObject {
private int _id;
private String dateCreated;
private String dateModified;
private String createModule;
private String modifiedModule;
// getters and setters here ...
}
Here's the super-class:
#DatabaseTable(tableName = "FOOD")
public class Food extends BaseObject {
public Food(int foodGroupId, String longDescription) {
this.foodGroupId = foodGroupId;
this.longDescription = longDescription;
}
private Integer foodGroupId;
private String longDescription;
// getters and setters here ...
}
Any help would be appreciated. Thank you.
This should work well with ORMLite as long as you annotate each of the fields in both the base-class and the super-class that you want persisted. For example, you need to need to add something like the following annotations to the BaseObject.
#DatabaseField(generatedId = true)
private int _id;
#DatabaseField
private String dateCreated;
#DatabaseField
private String dateModified;
#DatabaseField
private String createModule;
#DatabaseField
private String modifiedModule;
And then you add the following annotations to the super-class:
#DatabaseTable(tableName = "FOOD")
public class Food extends BaseObject {
#DatabaseField
private Integer foodGroupId;
#DatabaseField
private String longDescription;

Categories

Resources