链接多个转换.切换映射一个实时数据源



我正在为一个学校项目学习Android,我们必须使用Java,我们不能使用任何外部库。我正在创建一个大学生课程负载跟踪应用程序。 目前,我正在从事一项活动,该活动将详细说明用户选择的Course的信息。当用户选择Course时,我需要能够获取多个数据库结果:Mentor(讲师),Assessments的LiveDataListNotes的LiveDataList。我目前设置了一个Transformations.switchMap并正在努力获得CourseMentor。但是,LiveData似乎只能有一个转换观察者。这是我的代码:

课程详细视图模型

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 }

然后,我在填充 UI 的CourseDetailActivity中观察这些LiveData对象:Mentor填充集合,则选择SpinnerList<Assessment>List<Note>被传递到各自的RecyclerViewAdapter中。

我有一种感觉,我可以使用类似MediatorLiveData的东西,但是,即使在线查看了许多资源,我真的不完全了解如何正确使用它。我是Android的新手,这是我的第一个Android项目,所以我知道我有很多东西要学,我完全愿意接受设计决策的批评。

非常感谢您的帮助!

好的,我能够弄清楚!通过添加一个MutableLiveData<Long>来保存用于从数据库中检索Course的 ID,我能够从触发第一个Transformations.switchMap获取CURRENT_COURSEgetCourseById(long id)中设置值,从而触发COURSE_MENTORCOURSE_ASSESSMENTSCOURSE_NOTES的其他Transformations.switchMap

有人在其他地方建议我通过ViewModelConstructor传递 ID 并使用自定义ViewModelFactory,这是我将来会研究的事情。现在,我需要;)提交这个。

我希望这对其他人有所帮助!

课程详细视图模型

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}

相关内容

最新更新