将向日葵应用程序 ViewModel 更改为仅显示植物列表?
Changing the sunflower app ViewModel to only show a list of plants?
我正在尽力使用 Google 的向日葵应用程序作为我应用程序的基础,如何改变对变量 "growZoneNumber" 的依赖,使其仅 returns所有植物?
这是 java 从谷歌官方 kotlin 版本转换而来的代码:
package com.google.samples.apps.sunflower.viewmodels;
import com.google.samples.apps.sunflower.data.Plant;
import com.google.samples.apps.sunflower.data.PlantRepository;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations;
import androidx.lifecycle.ViewModel;
/**
* Created by Shawn Wang on 3/26/19.
*/
public class PlantListViewModel extends ViewModel {
private static final int NO_GROW_ZONE = -1;
private PlantRepository plantRepository;
private MutableLiveData<Integer> growZoneNumber;
public LiveData<List<Plant>> plants;
PlantListViewModel(@NonNull PlantRepository plantRepository) {
super();
this.plantRepository = plantRepository;
this.growZoneNumber = new MutableLiveData<>(-1);
this.plants = Transformations.switchMap(growZoneNumber, it -> {
if (it == NO_GROW_ZONE) {
return this.plantRepository.getPlants();
} else {
return this.plantRepository.getPlantsWIthGrowZoneNumber(it);
}
});
}
public void setGrowZoneNumber(int num) {
this.growZoneNumber.setValue(num);
}
public void cleanGrowZoneNumber() {
this.growZoneNumber.setValue(NO_GROW_ZONE);
}
public boolean isFiltered() {
return this.growZoneNumber.getValue() != NO_GROW_ZONE;
}
}
谢谢。
我花了一段时间才弄清楚但找到了答案(当然里面的模型略有不同
public class 固定视图模型扩展了 ViewModel {
private PinRepository pinRepository;
PinViewModel(@NonNull PinRepository pinRepository) {
this.pinRepository = pinRepository;
}
public LiveData<List<Pin>> getPins() {
return pinRepository.getPins();
}
}
我正在尽力使用 Google 的向日葵应用程序作为我应用程序的基础,如何改变对变量 "growZoneNumber" 的依赖,使其仅 returns所有植物?
这是 java 从谷歌官方 kotlin 版本转换而来的代码:
package com.google.samples.apps.sunflower.viewmodels;
import com.google.samples.apps.sunflower.data.Plant;
import com.google.samples.apps.sunflower.data.PlantRepository;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations;
import androidx.lifecycle.ViewModel;
/**
* Created by Shawn Wang on 3/26/19.
*/
public class PlantListViewModel extends ViewModel {
private static final int NO_GROW_ZONE = -1;
private PlantRepository plantRepository;
private MutableLiveData<Integer> growZoneNumber;
public LiveData<List<Plant>> plants;
PlantListViewModel(@NonNull PlantRepository plantRepository) {
super();
this.plantRepository = plantRepository;
this.growZoneNumber = new MutableLiveData<>(-1);
this.plants = Transformations.switchMap(growZoneNumber, it -> {
if (it == NO_GROW_ZONE) {
return this.plantRepository.getPlants();
} else {
return this.plantRepository.getPlantsWIthGrowZoneNumber(it);
}
});
}
public void setGrowZoneNumber(int num) {
this.growZoneNumber.setValue(num);
}
public void cleanGrowZoneNumber() {
this.growZoneNumber.setValue(NO_GROW_ZONE);
}
public boolean isFiltered() {
return this.growZoneNumber.getValue() != NO_GROW_ZONE;
}
}
谢谢。
我花了一段时间才弄清楚但找到了答案(当然里面的模型略有不同
public class 固定视图模型扩展了 ViewModel {
private PinRepository pinRepository;
PinViewModel(@NonNull PinRepository pinRepository) {
this.pinRepository = pinRepository;
}
public LiveData<List<Pin>> getPins() {
return pinRepository.getPins();
}
}