大家好,我在这里需要真正的帮助,我的整个应用程序都依赖于此。我正在尝试在 android 房间数据库中的 2 个实体之间创建一对多的关系。我想知道如何提取两个实体的数据,我可以显示狗的名字和所有者的名字,以及如何为此实现 recyclerview 适配器。以下代码是我从开发人员文档中得出的. 提前致谢
@Entity(tableName = "table_dog")
public class Dogs {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "dog_id")
private int dogId;
@ColumnInfo(name = "dog_name")
private String dogName;
@ColumnInfo(name = "dog_owner_id")
private int dogOwnerId;
public Dogs(String dogName, int dogOwnerId) {
this.dogName = dogName;
this.dogOwnerId = dogOwnerId;
}
public int getDogId() {
return dogId;
}
public void setDogId(int dogId) {
this.dogId = dogId;
}
public String getDogName() {
return dogName;
}
public void setDogName(String dogName) {
this.dogName = dogName;
}
public int getDogOwnerId() {
return dogOwnerId;
}
public void setDogOwnerId(int dogOwnerId) {
this.dogOwnerId = dogOwnerId;
}
}
@Entity(tableName = "table_dog")
public class Dogs {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "dog_id")
private int dogId;
@ColumnInfo(name = "dog_name")
private String dogName;
@ColumnInfo(name = "dog_owner_id")
private int dogOwnerId;
public Dogs(String dogName, int dogOwnerId) {
this.dogName = dogName;
this.dogOwnerId = dogOwnerId;
}
public int getDogId() {
return dogId;
}
public void setDogId(int dogId) {
this.dogId = dogId;
}
public String getDogName() {
return dogName;
}
public void setDogName(String dogName) {
this.dogName = dogName;
}
public int getDogOwnerId() {
return dogOwnerId;
}
public void setDogOwnerId(int dogOwnerId) {
this.dogOwnerId = dogOwnerId;
}
}
public class DogOwner {
@Embedded
public Owner owner;
@Relation(
parentColumn = "owner_id",
entityColumn = "dog_owner_id",
entity = Dogs.class
)
LiveData<List<Dogs>> dogs;
}
@Dao
public interface DogOwnerDao {
@Query("select * from table_owner")
LiveData<List<DogOwner>> getAllDogs();
@Insert
void insert(DogOwner dogOwner);
}