Dans mon application Android, im suivant les composants de l'architecture avec le modèle mvvm. mon application fait un appel réseau pour afficher l'information météo.appel est fait à partir du référentiel qui renvoie un livedata de réponse au viewmodel, qui est observé par mon activité principale.Observant viewmodel pour la deuxième fois renvoie null dans android
l'application fonctionne très bien, sauf pour une condition, chaque fois que je déconnecter l'Internet pour tester l'échec cas, il se gonfle vue d'erreur selon les besoins
dans la vue d'erreur j'ai un bouton de nouvelle tentative, ce qui rend l'appel de méthode observez à nouveau le viewmodel (cette méthode a également été appelée par oncreate() pour la première fois)
même après la mise en marche d'Internet, et en cliquant sur le bouton Réessayer qui écoute l'observable. Les données deviennent nulles.
Je ne sais pas why.please aider quelqu'un
REFERENTIEL
@Singleton public class ContentRepository {
@Inject AppUtils mAppUtils;
private RESTService mApiService;
@Inject public ContentRepository(RESTService mApiService) {
this.mApiService = mApiService;
}
public MutableLiveData<ApiResponse<WeatherModel>> getWeatherListData() {
final MutableLiveData<ApiResponse<WeatherModel>> weatherListData = new MutableLiveData<>();
mApiService.getWeatherList().enqueue(new Callback<WeatherModel>() {
@Override public void onResponse(Call<WeatherModel> call, Response<WeatherModel> response) {
weatherListData.setValue(new ApiResponse<>(response.body()));
}
@Override public void onFailure(Call<WeatherModel> call, Throwable t) {
weatherListData.setValue(new ApiResponse<>(t));
}
});
return weatherListData;
}
}
viewmodel
public class HomeViewModel extends AndroidViewModel {
private final LiveData<ApiResponse<WeatherModel>> weatherListObservable;
@Inject public HomeViewModel(Application application, ContentRepository contentRepository) {
super(application);
this.weatherListObservable = contentRepository.getWeatherListData();
}
public LiveData<ApiResponse<WeatherModel>> getWeatherListObservable() {
return weatherListObservable;
}
}
OBSERVER MÉTHODE DE L'ACTIVITÉ
private void observeViewModel() {
mHomeViewModel = ViewModelProviders.of(this, mViewModelFactory).get(HomeViewModel.class);
mHomeViewModel.getWeatherListObservable().observe(this, weatherModelApiResponse -> {
if (weatherModelApiResponse.isSuccessful()) {
mErrorView.setVisibility(View.GONE);
mBinding.ivLoading.setVisibility(View.GONE);
try {
setDataToViews(weatherModelApiResponse.getData());
} catch (ParseException e) {
e.printStackTrace();
}
} else if (!weatherModelApiResponse.isSuccessful()) {
mBinding.ivLoading.setVisibility(View.GONE);
mDialogUtils.showToast(this, weatherModelApiResponse.getError().getMessage());
mErrorView.setVisibility(View.VISIBLE);
}
});
}
BOUTON TENTATIVE DE L'ACTIVITÉ
@Override public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_retry:
mErrorView.setVisibility(View.GONE);
observeViewModel();
break;
}
}
@al_mukhtar Avez-vous trouvé une solution? –