[英]Android ExpandableListview with Room database
我需要填充一個ExpandableListView
其數據是從Room
數據庫中獲取的。
有關於如何使用SQLiteDatabase
做到這一點的答案:
1) Android ExpandableListView和SQLite數據庫
2) Android ExpandableListView從數據庫
使用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*/
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.