Android ExpandableListview with Room database - android

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*/
}

Related

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

Android working with realm

I have this model -
public class ItemsModel extends RealmObject {
private long id;
private String itemName;
private int itemCode;
private int itemPrice;
private int itemImage;
private int itemStock;
private int itemAmount;
private int discount;
private int total;
private boolean isSelected = false;
private int seller;
public ItemsModel() {
}
and this model -
public class CartModel extends RealmObject {
#PrimaryKey
private int cartId;
private RealmList<ItemsModel> itemsModels;
public CartModel() {
}
I saves items in ItemsModel and want to save the items selected in realmlist inside the CartModel, but there is some problems with that -
even when I'm trying to change the id before - it inserted to the main model(ItemsModel) and not to the ItemsList in the CartModel -
ItemsModel i = new ItemsModel();
i.setId(itemsModelRealmList.size());
i.setSeller(registerModels.getRegisterMainSeller());
i.setItemCode(item.getItemCode());
i.setItemPrice(item.getItemPrice());
i.setItemImage(item.getItemImage());
i.setItemName(item.getItemName());
realm.beginTransaction();
realm.where(CartModel.class).findFirst().getItemsModels().add(i);
realm.commitTransaction();
if I'm trying to insert to the list inside the cart the same object from ItemsModel - the recyclerview act like they are one object.
thanks

GreenDao list entity with a list of entities

I am making an Android application using GreenDao, and I have these two entities:
#Entity
public class Quiz {
#Id(autoincrement = true)
private Long id;
private Date date;
private String type;
#ToMany(referencedJoinProperty = "quizId")
private List<Answer> answers;
}
#Entity
public class Answer {
#Id(autoincrement = true)
private Long id;
private int answer;
private float value;
private int questionNumber;
private String type;
private Long quizId;
}
I'm trying to fetch the list of Quiz using the following code:
DaoSession daoSession = AndroidAdapter.getDaoSession();
QuizDao quizDao = daoSession.getQuizDao();
List<Quiz> quizs = quizDao.loadAll();
But the list of answers always comes empty, what am I doing wrong?
My AndroidAdapter class:
public class AndroidAdapter {
public static Context getContext() {
return AppApplication.getContext();
}
public static DaoSession getDaoSession() {
return AppApplication.getDaoSession();
}
}
And AppApplication class
#Override
public void onCreate() {
super.onCreate();
context = this;
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this,ENCRYPTED ? "exam-db-encrypted" : "exam-db");
Database db = ENCRYPTED ? helper.getEncryptedWritableDb("exam-secret") : helper.getWritableDb();
daoSession = new DaoMaster(db).newSession();
this.addAllEvents();
}
public static DaoSession getDaoSession() {
return daoSession;
}
For me, I've not entered some Answer entities in my db.
Once I've added a code to add those entities to the db and I've requested the list with a generated getter for the List<Answer> answers - it all worked as it should 😊

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
}

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