我试图将ViewModel与一些使用来自另一个LiveData的值的LiveData一起使用。
为此,我试图使用Transformations.switchMap,但我得到了
不兼容的类型。必需的MutableLiveData但“switchMap”被推断为LiveData:不存在变量Y的实例,因此LiveData符合MutableLiveData
我已经尝试切换到Transformations.map,但结果是一样的。
public class RestaurantViewModel extends ViewModel {
private MutableLiveData<FirebaseUser> userLiveData = new MutableLiveData<>();
private final MutableLiveData<String> userId =
Transformations.switchMap(userLiveData, input -> {
return input.getUid();
});
private String getUid(FirebaseUser user){
return user.getUid();
}
private void setUser(FirebaseUser currentUser){
this.userLiveData.setValue(currentUser);}
}我希望userId依赖于userLiveData的值,但我无法做到这一点。
发布于 2018-12-27 05:15:18
基本上,Transformations.switchMap返回LiveData,而接收器类型是MutableLiveData。考虑修改如下:
private final LiveData<String> userId = Transformations.switchMap(userLiveData, input -> {
return input.getUid();
}); // Here we change userId 'MutableLiveData' to 'LiveData'.检查参考文献。
https://stackoverflow.com/questions/53939760
复制相似问题