Android 带有 Room 数据库的 ExpandableListview

Android ExpandableListview with Room database

我需要填充一个 ExpandableListView,其数据是从 Room 数据库中获取的。

有关于如何使用 SQLiteDatabase 执行此操作的答案:

1) Android ExpandableListView and SQLite Database

2) Android ExpandableListView From Database

是否可以使用 Room Database 实现相同的功能?

我有两个表: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 */
}

有什么建议吗?

使用 @Relation 找到了解决方案。

参考: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*/
}