我需要填充一个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 */
}
有什么建议吗?
答案 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*/
}