0
votes

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?

1

1 Answers

0
votes

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