Java Android LiveData:Transformation switchMap:在原始列表上应用过滤器并显示过滤后的数据
请阅读本文并将switchMap转换应用于源livedata。它不会改变原始源LiveData值。您不应重新创建LiveData实例。让它们成为最终版本。感谢您的评论。尝试但它仍然没有按要求工作。主livedata列表正在被筛选,在任何情况下都不应被更改。谢谢,您能在这里帮助我应用switchmap吗,因为我不知道如何在nonstop()中的代码中使用它。mSearchFlights-这是原始livedata。Java Android LiveData:Transformation switchMap:在原始列表上应用过滤器并显示过滤后的数据,java,android,android-livedata,android-architecture-components,android-mvvm,Java,Android,Android Livedata,Android Architecture Components,Android Mvvm,请阅读本文并将switchMap转换应用于源livedata。它不会改变原始源LiveData值。您不应重新创建LiveData实例。让它们成为最终版本。感谢您的评论。尝试但它仍然没有按要求工作。主livedata列表正在被筛选,在任何情况下都不应被更改。谢谢,您能在这里帮助我应用switchmap吗,因为我不知道如何在nonstop()中的代码中使用它。mSearchFlights-这是原始livedata。 public class FlightViewModel extends BaseV
public class FlightViewModel extends BaseViewModel {
private FlightRepository flightRepository;
private MediatorLiveData<Resource<FlightSearchMainOuterModel>> mSearchFlights = new MediatorLiveData<>();
private MediatorLiveData<Resource<FlightSearchMainOuterModel>> mOriginalList = new MediatorLiveData<>();
private MediatorLiveData<Resource<FlightSearchMainOuterModel>> mSortedSearchFlights = new MediatorLiveData<>();
public FlightViewModel(@NonNull Application application) {
super(application);
flightRepository = FlightRepository.getInstance(application);
}
@Override
public void updateResults() {
}
public void postFlightSearch() {
mSearchFlights.addSource(flightRepository.postFlightSearchData(requestJson), mSearchFlights::setValue);
}
public LiveData<Resource<FlightSearchMainOuterModel>> getFlightResult() {
return Transformations.map(mSearchFlights, input -> {
if (input == null || input.data == null || input.status != Resource.Status.SUCCESS)
return null;
if (input.status == Resource.Status.SUCCESS && input.data != null) {
if (input.data.getError().getErrorCode().equalsIgnoreCase("1")) {
FlightSearchModel flightSearchModel;
List<FlightSearchMainOuterResultOnwordReturnModel> onword = input.data.getResults().getOnword();
for (FlightSearchMainOuterResultOnwordReturnModel onwordLiveData : onword) {
flightSearchModel = onwordLiveData.getSegments().get(0);
flightSearchModel.getDurationFormat(onwordLiveData.getSegments());
}
return Resource.cloneResource(input, input.data);
}
} else if(input.status == Resource.Status.LOADING){
return Resource.loading(null);
} else {
return Resource.error("Error! Please try again.", null);
}
return null;
});
}
public void copyToOrignal(){
mOriginalList = new MediatorLiveData<>();
mOriginalList.setValue(mSearchFlights.getValue());
}
public LiveData<Resource<FlightSearchMainOuterModel>> getSortedFlightResult() {
return mSortedSearchFlights;
}
public void nonStop(boolean isOneStop) {
copyToOrignal();
LiveData<Resource<FlightSearchMainOuterModel>> onwordLiveData = Transformations.map(mOriginalList, input -> {
if (input == null || input.data == null || input.status != Resource.Status.SUCCESS)
return null;
List<FlightSearchMainOuterResultOnwordReturnModel> onwordNewList = new ArrayList<>();
List<FlightSearchMainOuterResultOnwordReturnModel> onword = input.data.getResults().getOnword();
if (onword.size() > 0) {
if(isOneStop){
for(int i =0; i<onword.size(); i++){
if(onword.get(i).getSegments().size()>1 || !onword.get(i).getSegments().get(0).getNumberofStops().equalsIgnoreCase("0")){
onwordNewList.add(onword.get(i));
}
}
}else {
for(int i =0; i<onword.size(); i++){
if(onword.get(i).getSegments().size()==1){
if(onword.get(i).getSegments().get(0).getNumberofStops().equalsIgnoreCase("0")){
onwordNewList.add(onword.get(i));
}
}
}
}
input.data.getResults().setOnword(onwordNewList);
}
return Resource.cloneResource(input, input.data);
});
mSortedSearchFlights.addSource(onwordLiveData, mSortedSearchFlights::setValue);
}
}
flightViewModel.getFlightResult().observe(this, flightSearchModelResource -> {
if (flightSearchModelResource == null)
return;
if (flightSearchModelResource.status == Resource.Status.SUCCESS && flightSearchModelResource.data != null) {
if (flightSearchModelResource.data.getError().getErrorCode().equalsIgnoreCase("1")) {
firstOrderAdapter.setData(flightSearchModelResource.data.getResults().getOnword());
} else {
Utils.toastLong(getContext(), flightSearchModelResource.data.getError().getErrorMessage());
}
}
});
flightViewModel.getSortedFlightResult().observe(this, flightSearchModelResource -> {
if (flightSearchModelResource == null)
return;
if (flightSearchModelResource.status == Resource.Status.SUCCESS && flightSearchModelResource.data != null) {
if (flightSearchModelResource.data.getError().getErrorCode().equalsIgnoreCase("1")) {
if(flightSearchModelResource.data.getResults().getOnword().size()>0){
firstOrderAdapter.setData(flightSearchModelResource.data.getResults().getOnword());
}
} else {
Utils.toastLong(getContext(), flightSearchModelResource.data.getError().getErrorMessage());
}
}
});