gpt4 book ai didi

java - 链接多个 Transformations.switchMap 关闭一个 LiveData 源

转载 作者:行者123 更新时间:2023-12-01 16:59:46 25 4
gpt4 key购买 nike

我正在为学校项目学习 Android,我们必须使用 Java,并且不能使用任何外部库。我正在创建一个大学生类(class)负载跟踪应用程序。目前,我正在开展一项 Activity ,该 Activity 将详细说明 Course 的信息。由用户选择。当用户选择 Course 时,我需要能够获取多个数据库结果:Mentor (讲师),a LiveData ListAssessment s 和 a LiveData ListNote s。我目前有一个Transformations.switchMap设置并努力获取 Course Mentor 。然而,它似乎是 LiveData其上只能有一个该变换观察者。这是我的代码:

类(class)详细信息 View 模型

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 }

然后我观察这些LiveData我的CourseDetailActivity中的对象填充 UI:Mentor填充 a 设置 a Spinner 的选择, List<Assessment>List<Note>被传递到各自的 RecyclerView Adapter s。

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

非常感谢您的帮助!

最佳答案

好吧,我终于明白了!通过添加MutableLiveData<Long>保存用于检索 Course 的 ID从数据库中,我可以设置 getCourseById(long id) 中的值这会触发第一个 Transformations.switchMap获取CURRENT_COURSE这引发了另一个 Transformations.switchMapCOURSE_MENTOR , COURSE_ASSESSMENTSCOURSE_NOTES .

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

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

类(class)详细信息 View 模型

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}

关于java - 链接多个 Transformations.switchMap 关闭一个 LiveData 源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61527583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com