简体   繁体   English

带有Room数据库的Android ExpandableListview

[英]Android ExpandableListview with Room database

I need to populate an ExpandableListView whose data is fetched from Room database. 我需要填充一个ExpandableListView其数据是从Room数据库中获取的。

There are answers on how to do this with SQLiteDatabase : 有关于如何使用SQLiteDatabase做到这一点的答案:

1) Android ExpandableListView and SQLite Database 1) Android ExpandableListView和SQLite数据库

2) Android ExpandableListView From Database 2) Android ExpandableListView从数据库

Is it possible to achieve the same with Room Database? 使用Room Database可以达到相同的目的吗?

I have two tables: a) GroupHeader b) GroupContent 我有两个表: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 . 使用@Relation找到了解决方案。

Reference: https://developer.android.com/reference/android/arch/persistence/room/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*/
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM