不存在类型变量 Y 的实例,因此 Calendar 符合 LiveData<Y>

No instance(s) of type variable(s) Y exist so that Calendar conforms to LiveData<Y>

我希望我的 LiveData<Calendar> 实例在每次 ListAlarms 更改时更改。我有以下代码:

private LiveData<Calendar> nextAlarm;

public MyViewModel(@NonNull Application app) {

    nextAlarm = Transformations.switchMap(alarmRepo.getAlarms(), alarms -> {
        Calendar nearest = null;
        // ... some necessary computation here (iterating through alarms):
        // foreach alarms, find nearest one, create new Calendar according to day and time saved in database
        return nearest;
    });

我看到的错误:

No instance(s) of type variable(s) Y exist so that Calendar conforms

有人可以帮我吗?我如何 return 在 SwitchMapFunction 中新建日历实例?

to LiveData

我认为你需要使用 Transformation.map() 函数而不是 Transformation.switchMap()