1

I've followed https://github.com/googlecodelabs/android-build-an-app-architecture-components.

I want to be able to fetch weather data by city name.

I've created the required method in the DAO class.

I've changed my code in the Repository class to:

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

But in my ViewModel class when I'm trying to use this function in the Transformation.switchMap, Im getting compile time error that the method getCurrentWeatherForecasts(String ) cannot be applied to getCurrentWeatherForecasts().

Here's my code in ViewModel class:

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

I've read Android's Transformations.switchMap docs, but I couldn't figure out what am I doing wrong.

Can anyone explain me what's wrong with my code.

user1080528
  • 75
  • 1
  • 6
  • First argument in switch map is the source that should be live data which works as a trigger live data... So you have to add your forecast method as the first argument and then you can set the value in the live data in which you want to transform... If any further query, you can ask.. – Abhishek Sharma Sep 21 '18 at 09:03
  • According to Android Transformations.switchMap docs, The first should be a LiveData, as in my code. You can see it here: https://developer.android.com/reference/android/arch/lifecycle/Transformations.html#switchMap – user1080528 Sep 23 '18 at 05:41
  • First argument needs to be the source trigger, as @AbhishekSharma said. :) – Mikkel Larsen Apr 02 '19 at 06:52

0 Answers0