Android Transformations.switchMap实现

问题描述 投票:0回答:1

我关注了https://github.com/googlecodelabs/android-build-an-app-architecture-components

我希望能够按城市名称获取天气数据。

我已经在DAO类中创建了所需的方法。

我已将存储库类中的代码更改为:

 public LiveData<List<ListWeatherEntry>> getCurrentWeatherForecasts(String cityName) {
    initializeData();
    Date today = SunshineDateUtils.getNormalizedUtcDateForToday();
    return mWeatherDao.getCurrentWeatherForecasts(today,cityName);
}

但是在我的ViewModel类中,当我尝试在Transformation.switchMap中使用此函数时,出现了编译时错误,即方法getCurrentWeatherForecasts(String)无法应用于getCurrentWeatherForecasts()。

这是我在ViewModel类中的代码:

   private final SunshineRepository mRepository;
    public  LiveData<List<ListWeatherEntry>> mForecast;
    private final MutableLiveData<String> cityName = new MutableLiveData();
    public MainActivityViewModel(SunshineRepository repository) {
        this.mRepository = repository;
        mForecast  = Transformations.switchMap(this.cityName,(city)-> 
        mRepository.getCurrentWeatherForecasts(city)); 
    }

我已经阅读了Android的Transformations.switchMap文档,但无法弄清楚我在做什么错。

谁能解释我的代码有什么问题。

android android-livedata
1个回答
0
投票

Android LiveData: Transformation switchMap: Apply filter on the original list and show the filtered data

任何人都可以帮助我实现切换图

在提到的链接中,原始liveData源是mSearchFlights,如建议使用switchmap的,所以我尝试了此操作:

 public void nonStop(boolean isOneStop) {

        LiveData<Resource<FlightSearchMainOuterModel>> onwordLiveData =  Transformations.switchMap(mOriginalList, new Function<Resource<FlightSearchMainOuterModel>, LiveData<Resource<FlightSearchMainOuterModel>>>() {
            @Override
            public LiveData<Resource<FlightSearchMainOuterModel>> apply(Resource<FlightSearchMainOuterModel> input) {


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


    }

但不起作用。请帮助!

© www.soinside.com 2019 - 2024. All rights reserved.