RealmDB custom fields - android

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
}

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;
//...
}

How to insert data and retrieve data in nested relationships in Room Database Android

I have entities
#Entity
public class A {
#PrimaryKey(autoGenerate = true)
public long id;
public String bFId;
public List<B> bList;
}
#Entity()
public class B {
#PrimaryKey #NonNull
public String id;
public String oneCId;
public List<C> cList;
}
#Entity
public class C {
#PrimaryKey #NonNull
public String id;
public String value;
}
And I wrote this like a Relation
public class AWithB extends A{
#Relation(parentColumn = "id", entityColumn = "bId")
public List<BWithC> bWithC;
}
public class BWithC {
#Embedded
public B b;
#Relation(entity=C.class,parentColumn="bFId",entityColun="cid")
public List<C> {}
}
Is my relation wrong? And How to insert data And retrieve data this.I must to be used this relation.I cannot use another relation.Please help me.

Realm query with child arrays

Here's the Object I'm working with:
public class NavigationMenuModule extends RealmObject implements Parcelable {
#PrimaryKey
public String sectionKey;
public RealmList<ItemModule> modules;
public RealmList<Article> spotlightSponsored;
public RealmList<Article> items;
}
The child Article Object:
public class Article extends RealmObject {
#PrimaryKey
public String contentId;
public String leadImageURL;
public String summary;
public String headline;
}
How would I structure this realm call:
get NavigationMenuModule item by sectionKey
get spotlightSponsored within that NavigationMenuModule that matches the article's contentId
The method below works but I feel like there's probably a "neater" way:
public static Article getArticle(String sectionKey, String articleId) {
Realm realm = Realm.getDefaultInstance();
NavigationMenuModule navigationMenuModule = realm.where(NavigationMenuModule.class).equalTo("sectionKey", sectionKey).findFirst();
if (navigationMenuModule != null && !navigationMenuModule.spotlightSponsored.isEmpty()) {
for (Article article : navigationMenuModule.spotlightSponsored) {
if (article.getContentId().equals(articleId)) {
Article ret = realm.copyFromRealm(article);
realm.close();
return ret;
}
}
}
realm.close();
return null;
}
Theoretically this should work with Realm 3.5.0
public class NavigationMenuModule extends RealmObject implements Parcelable {
#PrimaryKey
public String sectionKey;
public RealmList<ItemModule> modules;
public RealmList<Article> spotlightSponsored;
public RealmList<Article> items;
}
public class Article extends RealmObject {
#PrimaryKey
public String contentId;
public String leadImageURL;
public String summary;
public String headline;
#LinkingObjects("spotlightSponsored")
public final RealmResults<NavigationMenuModule> spotlightSponsoredOf = null;
#LinkingObjects("items")
public final RealmResults<NavigationMenuModule> itemsOf = null;
}
public static Article getArticle(Realm realm, String sectionKey, String articleId) {
return realm.where(Article.class)
.equalTo("contentId", articleId)
.equalTo("spotlightSponsoredOf.sectionKey", sectionKey)
.findFirst();
}

Android - Save the realmlist inside a realm object

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();

Categories

Resources