无法在UI中观察实时数据列表

时间:2018-11-14 16:30:52

标签: android-room android-livedata mutablelivedata

在我的viewModel中,我收到一个LiveData对象,其中包含自制菜肴以及组成每个菜肴的成分。我想将食材与餐具分开,以便我的UI可以将数据发送到适配器,适配器将在包含2个回收站视图的卡片视图中显示餐具和每个餐具的相应成分。 但是,当我从菜肴中分离出食材并将其添加到Livedata列表类型时,我再也无法在活动中观察到它了。我该如何实现?

list of dishes expanded cardView should show ingredients for the dish

    //in DishesViewModel 
    //getter for all the dishes from the repository
public List<LiveData<List<DishWithIngredients>>> getAllDishes() {

    LiveData ingredients = Transformations.map(mAllDishes, input ->
            Objects.requireNonNull(mAllDishes.getValue()).listIterator().next().ingredients);

    LiveData dishes = Transformations.map(mAllDishes, input ->
            Objects.requireNonNull(mAllDishes.getValue()).listIterator().next().dishEntry);

    List<LiveData<List<DishWithIngredients>>> result = new ArrayList<>();
    result.add(ingredients);
    result.add(dishes);

    return result;
}

执行上述操作后,我将无法再在UI中观察数据。无法解决方法观察

//MainActivity
 mDishesViewModel.getAllDishes().observe(this, (Observer<List<DishWithIngredients>>) 
            dishWithIngredients -> mDishesAdapter.swapDishes(dishWithIngredients));

0 个答案:

没有答案