我正在为一个学校项目学习Android,我们必须使用Java,我们不能使用任何外部库。我正在创建一个大学生课程负载跟踪应用程序。目前,我正在处理一个活动,该活动将详细介绍用户选择的Course的信息。当用户选择讲师时,我需要能够获取多个数据库结果:Mentor (讲师)、LiveData List of Assessments和LiveData List of Notes。我目前设置了一个Transformations.switchMap,正在努力获取Course Mentor。然而,似乎LiveData上只能有一个这样的转换观察者。下面是我的代码:
CourseDetailViewModel
LiveData<Course> currentCourse;
LiveData<Mentor> courseMentor;
LiveData<List<Assessment>> courseAssessments;
LiveData<List<Note>> courseNotes;
final CourseDetailRepository REPO;
public CourseDetailViewModel(@NonNull Application application) {
REPO = new CourseDetailRepository(application);
}
public LiveData<Course> getCurrentCourse(long id) {
if (currentCourse == null) {
currentCourse = REPO.getCourseById(id);
}
// I'm dong the Transformations here because I tried in the Constructor but because currentCourse
// was null, the transformation wasn't kicking off.
// the courseMentor Transformation works, the others don't seem to fire.
courseMentor = Transformations.switchMap(curentCourse, course ->
REPO.getMentorById(course.getMentorId()));
courseAssessments = Transformations.switchMap(currentCourse, course ->
REPO.getAssessmentsByCourse(course.getCourseId()));
courseNotes = Transformations.switchMap(currentCourse, course ->
REPO.getNotesByCourse(course.getCourseId())));
return this.currentCourse;
}
public LiveData<Mentor> getCourseMentor() { return this.courseMentor }
public LiveData<List<Assessment>> getCourseAssessments() { return this.courseAssessments }
public LiveData<List<Note>> getCourseNotes() { return this.courseNotes }然后,我观察CourseDetailActivity中的这些LiveData对象来填充UI:Mentor填充一组Spinner,List<Assessment>和List<Note>被传递到它们各自的RecyclerView Adapter中。
我感觉我可以使用像MediatorLiveData这样的东西,但是,我真的不完全理解如何正确地使用它,即使在网上查看了许多资源之后也是如此。我刚接触Android,这是我的第一个Android项目,所以我知道我有很多东西要学,我对设计决策的批评是完全开放的。
非常感谢您的帮助!
发布于 2020-05-02 01:22:24
好的,所以,我能够弄清楚了!通过添加一个MutableLiveData<Long>来保存用于从数据库中检索Course的ID,我能够设置getCourseById(long id)中的值,该值触发第一个Transformations.switchMap以获取CURRENT_COURSE,从而触发COURSE_MENTOR、COURSE_ASSESSMENTS和COURSE_NOTES的其他Transformations.switchMap。
其他地方有人建议我通过ViewModel Constructor传递ID,并使用自定义的ViewModelFactory,这是我将来会研究的。现在,我需要提交这个;)。
我希望这对其他人有帮助!
CourseDetailViewModel
final MutableLiveData<Long> CURRENT_COURSE_ID;
final LiveData<Course> CURRENT_COURSE;
final LiveData<Mentor> COURSE_MENTOR;
final LiveData<List<Assessment>> COURSE_ASSESSMENTS;
final LiveData<List<Note>> COURSE_NOTES;
final CourseDetailRepository REPO;
public CourseDetailViewModel(@NonNull Application application) {
REPO = new CourseDetailRepository(application);
CURRENT_COURSE_ID = new MutableLiveData<>();
CURRENT_COURSE = Transformations.switchMap(CURRENT_COURSE_ID,
COURSE_ID -> REPO.getCourseById(COURSE_ID));
COURSE_MENTOR = Transformations.switchMap(CURRENT_COURSE,
COURSE -> REPO.getMentorById(COURSE.getMentorId()));
COURSE_ASSESSMENTS = Transformations.switchMap(CURRENT_COURSE,
COURSE -> REPO.getAssessmentsByCourse(COURSE.getCourseId()));
COURSE_NOTES = Transformations.switchMap(CURRENT_COURSE,
COURSE -> REPO.getNotesByCourse(COURSE.getCourseId())));
}
public LiveData<Course> getCurrentCourse(long id) {
CURRENT_COURSE_ID.setValue(id);
return this.CURRENT_COURSE;
}
public LiveData<Mentor> getCourseMentor() { return this.COURSE_MENTOR}
public LiveData<List<Assessment>> getCourseAssessments() { return this.COURSE_ASSESSMENTS}
public LiveData<List<Note>> getCourseNotes() { return this.COURSE_NOTES}https://stackoverflow.com/questions/61527583
复制相似问题