LiveData,Transformations.map() - 当用户更改过滤顺序时如何强制刷新?

我使用LiveData+ Transformations.map():


private final LiveData<List<Task>> observableTasks;

(...)

observableTasks =  Transformations.map(tasksRepository.getTasks(), tasks-> filterTasks(tasks));

如何强制LiveData刷新?我需要Transformations.map()准备新的清单。当用户更改过滤选项时,我需要filterTasks(tasks)再次调用并显示新的排序列表。来自存储库 ( tasksRepository.getTasks()) 的数据保持不变。


智慧大石
浏览 118回答 3
3回答

眼眸繁星

根据您提供的信息,我执行了以下步骤,并且我的 observable 已按预期触发。我认为您在存储库或处理转换的类中做错了什么。由于没有足够的代码检查,我创建了自己的虚拟类:我创建了虚拟任务 POJO:public class Task {&nbsp; &nbsp; private String id;&nbsp; &nbsp; public Task(String id) {&nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; }&nbsp; &nbsp; public String getId() {&nbsp; &nbsp; &nbsp; &nbsp; return id;&nbsp; &nbsp; }&nbsp; &nbsp; public void setId(String id) {&nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; }&nbsp; &nbsp; @NonNull&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return "Task id is " +id;&nbsp; &nbsp; }}我创建了具有 LiveData 的虚拟存储库:public class TasksRepository {&nbsp; &nbsp; private List<Task> taskList = new ArrayList<>();&nbsp; &nbsp; private MutableLiveData<List<Task>> _tasks = new MutableLiveData<>();&nbsp; &nbsp; private LiveData<List<Task>> tasks = _tasks;&nbsp; &nbsp; public TasksRepository() {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 5; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; taskList.add(new Task(createTaskId()));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; _tasks.setValue(taskList);&nbsp; &nbsp; }&nbsp; &nbsp; private String createTaskId() {&nbsp; &nbsp; &nbsp; &nbsp; return UUID.randomUUID().toString();&nbsp; &nbsp; }&nbsp; &nbsp; public void addTask() {&nbsp; &nbsp; &nbsp; &nbsp; taskList.add(new Task(createTaskId()));&nbsp; &nbsp; &nbsp; &nbsp; _tasks.setValue(taskList);&nbsp; &nbsp; }&nbsp; &nbsp; public LiveData<List<Task>> getTasks() {&nbsp; &nbsp; &nbsp; &nbsp; return tasks;&nbsp; &nbsp; }}创建了一个名为 MyViewModel 的类,它可以处理转换。在转换过程中,我们只需将“TEST”前缀添加到任务 ID。您可以将其与您的代码进行比较,它应该没问题:public class MyViewModel {&nbsp; &nbsp; private final LiveData<List<Task>> observableTasks;&nbsp; &nbsp; public MyViewModel(TasksRepository tasksRepository) {&nbsp; &nbsp; &nbsp; &nbsp; this.observableTasks = Transformations.map(tasksRepository.getTasks(), this::changeId);&nbsp; &nbsp; }&nbsp; &nbsp; private List<Task> changeId(List<Task> tasks) {&nbsp; &nbsp; &nbsp; &nbsp; List<Task> resultTaks = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; for (Task task : tasks) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String newId = "TASK" + task.getId();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; task.setId(newId);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resultTaks.add(task);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return resultTaks;&nbsp; &nbsp; }&nbsp; &nbsp; public LiveData<List<Task>> getObservableTasks() {&nbsp; &nbsp; &nbsp; &nbsp; return observableTasks;&nbsp; &nbsp; }}在单击按钮时添加数据并观察数据变化:TasksRepository tasksRepository = new TasksRepository();MyViewModel viewModel = new MyViewModel(tasksRepository);button.setOnClickListener(v -> tasksRepository.addTask());viewModel.getObservableTasks().observe(this,&nbsp; &nbsp; &nbsp; &nbsp; tasks -> Log.d(TAG, Arrays.toString(new List[]{tasks})));

一只萌萌小番薯

我想我找到了解决方案。我创建了附加LiveData字段filterChangeEvent。每次用户更改过滤顺序时,都会将新值设置为filterChangeEvent。然后我用switchMapwithfilterChangeEvent作为触发器:&nbsp; &nbsp; observableTasks = Transformations.switchMap(filterChangeEvent, input -> {&nbsp; &nbsp; &nbsp; &nbsp; final MediatorLiveData<List<Tasks>> result = new MediatorLiveData<>();&nbsp; &nbsp; &nbsp; &nbsp; result.addSource(tasksRepository.getTasks(), tasks -> result.setValue(filterTasks(tasks)));&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; });

慕尼黑8549860

设想:在存储库包含 User(1, "Jane") 和 User(2, "John") 的场景中,当 userIdLiveData 值设置为“1”时,switchMap 将调用 getUser(1),这将返回一个 LiveData包含值 User(1, "Jane")。所以现在,userLiveData 将发出 User(1, "Jane")。当存储库中的用户更新为 User(1, "Sarah") 时,userLiveData 会自动收到通知并发出 User(1, "Sarah")。当使用 userId = "2" 调用 setUserId 方法时,userIdLiveData 的值会发生变化并自动触发从存储库获取 id 为 "2" 的用户的请求。因此,userLiveData 发出 User(2, "John")。repository.getUserById(1) 返回的 LiveData 作为源被删除。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java