gpt4 book ai didi

java - Android 实时数据 : Transformation switchMap: Apply filter on the original list and show the filtered data

转载 作者:行者123 更新时间:2023-12-01 18:36:58 25 4
gpt4 key购买 nike

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);

}

}

在 mSearchFlights 中,我得到了必须应用过滤器的整个列表。应用一个过滤器后,即使将数据复制到另一个 liveData 中,mSearchFlights 原始列表也会被过滤。因此,当第二次应用过滤器时,它会作用于过滤后的列表,而不是原始列表(即 mSearchFlights)。所以请帮助我应用过滤器。

在 fragment 中,我观察到两个相同的实时数据:

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());

}
}
});

最佳答案

请阅读此document并将 switchMap 转换应用于您的源实时数据。它不会改变您的原始源 LiveData 值。

关于java - Android 实时数据 : Transformation switchMap: Apply filter on the original list and show the filtered data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60014677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com