我是新来的房间,我正在努力。我想做一个简单的选择在两个名为项目和任务表。这是两个类和第三个连接它们的类:
@Entity
public class Project {
@PrimaryKey(autoGenerate = true)
private final long id;
@NonNull
private final String name;
@ColorInt
private final Integer color;
}
@Entity(foreignKeys = @ForeignKey(entity = Project.class, parentColumns = "id", childColumns = "projectId"))
public class Task {
@PrimaryKey(autoGenerate = true)
private long idTask;
// clé commune
private long projectId;
@NonNull
private String nameTask;
public long creationTimestamp;
// constructor, getters, setters
}
public class TaskWithProject {
@Embedded public Project project;
@Relation(parentColumn = "id", entityColumn = "projectId")
public Task task;
}
我在我的刀课上尝试了很多东西。以下是其中两个:
@Dao
public interface TaskDao {
(...)
// Solution 1
@Query("SELECT * FROM Task t JOIN Project p ON t.projectId = p.id")
LiveData<List<TaskWithProject>> getTaskWithProject();
// Solution 2 from https://developer.android.com/training/data-storage/room/relationships
@Transaction
@Query("SELECT * FROM Task")
LiveData<List<TaskWithProject>> getTaskWithProject();
我的数据库中填充了这些数据(在数据库检查器中没有问题):
projectDao.insertProject(new Project(0, "Projet Tartampion", 0xFFEADAD1));
projectDao.insertProject(new Project(0, "Projet Lucidia", 0xFFB4CDBA));
projectDao.insertProject(new Project(0, "Projet " + "Circus", 0xFFA3CED2));
taskDao.insertTask(new Task(0, 1, "Task 1 - Tartampion"));
taskDao.insertTask(new Task(0, 1, "Task 2 - Tartampion"));
taskDao.insertTask(new Task(0, 2, "Task 1 - Lucidia"));
我用这个函数获取数据:
public LiveData<List<TaskViewStateItem>> getAllTasks() {
return Transformations.map(repository.getTaskWithProject(), tasks -> {
List<TaskViewStateItem> liststateitems = new ArrayList<>();
for (TaskWithProject t : tasks) {
Log.i(TAG, "getAllTasks: "+ t.toString());
liststateitems.add(new TaskViewStateItem(t.task.getIdTask(), t.task.getNameTask(), t.project.getName(), t.project.getColor(), t.task.getCreationTimestamp()));
}
}
return liststateitems;
});
}
根据上面的解决方案1,我有3个条目,但有一个重复的。
I/Log ViewModel: getAllTasks: TaskWithProject{project=Project{id=1, name='Projet Tartampion', color=-1385775}, task=Task{idTask=2, projectId=1, nameTask='Task 2 - Tartampion', creationTimestamp=1634723235}}
I/Log ViewModel: getAllTasks: TaskWithProject{project=Project{id=1, name='Projet Tartampion', color=-1385775}, task=Task{idTask=2, projectId=1, nameTask='Task 2 - Tartampion', creationTimestamp=1634723235}}
getAllTasks: TaskWithProject{project=Project{id=2, name='Projet Lucidia', color=-4928070}, task=Task{idTask=3, projectId=2, nameTask='Task 1 - Lucidia', creationTimestamp=1634723235}}
解决方案2不编译
error: Not sure how to convert a Cursor to this method's return type (com.cleanup.todoc.model.TaskWithProject).
LiveData<List<TaskWithProject>> getTaskWithProject();
^
我不知道我做错了什么。所以非常感谢你给我的任何帮助
由于您的TaskWithProject类嵌入了项目,因此它期望获得项目的列,然后从中构建任务(s),因此当您使用SELECT * FROM Task
时,它没有相关的列(与连接它确实但可能不像预期的那样工作)。
- TaskWithProject是ProjectWithTask.
所以使用SELECT * FROM project
或使用POJO与任务嵌入和项目作为@关系(与SELECT * FROM task
)。