带有Room数据库的Android ExpandableListview

时间:2018-12-02 09:15:50

标签: android android-room android-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 */
}

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

使用@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*/
}