我想在ViewModel类的改装失败时显示Toast消息
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我想在ViewModel类的改装失败时显示Toast消息相关的知识,希望对你有一定的参考价值。
[您好,我有这个应用程序,它在其中显示来自TMDB的电影,我遇到了一个问题,我无法向用户显示反馈,这可能导致他们不必要地等待我的应用程序在没有互联网或服务器未返回任何数据的情况下启动
public MainViewModel(@NonNull Application application)
super(application);
AppDatabase appDatabase = AppDatabase.getInstance(this.getApplication());
favoriteMovies = appDatabase.favoriteDao().loadAllFavorites();
Call<ApiResults> call = Network.buildAPICall(Network.POPULAR);
call.enqueue(new Callback<ApiResults>()
@Override
public void onResponse(Call<ApiResults> call, Response<ApiResults> response)
if (response.message().contentEquals("OK"))
popularMovies.setValue(response.body().getMovies());
else
Log.e(TAG, "Something unexpected happened to our request: " + response.message());
@Override
public void onFailure(Call<ApiResults> call, Throwable t)
Log.i(TAG, "Something unexpected happened to our request: " );
Log.e(TAG, t.getMessage());
);
我想显示消息“我们的请求发生了意外:”,当无法通过mainActivity访问Internet时,问题是我无法在视图模型类中显示烤面包机这是我的主要活动代码片段
public void setupViewModel()
com.example.popularmovies.UI.MainViewModel viewModel = ViewModelProviders.of(this).get(com.example.popularmovies.UI.MainViewModel.class);
Log.i("Test",""+ viewModel);
viewModel.getFavoriteMovies().observe(this, new Observer<List<MovieData>>()
@Override
public void onChanged(@Nullable List<com.example.popularmovies.Data.MovieData> favoriteEntries)
Log.d(TAG, "Receiving changes from LiveData");
if (mSortOrder.contentEquals(FAVORITE))
List<com.example.popularmovies.Data.MovieData> movieList = new ArrayList<com.example.popularmovies.Data.MovieData>();
if (favoriteEntries != null)
for (com.example.popularmovies.Data.MovieData fave : favoriteEntries)
fave.setFavorite(1);
setAdapter(favoriteEntries);
);
viewModel.getTopRatedMovies().observe(this, new Observer<List<com.example.popularmovies.Data.MovieData>>()
@Override
public void onChanged(@Nullable List<com.example.popularmovies.Data.MovieData> movieData)
Log.i("Test",""+ movieData);
if (movieData != null && mSortOrder.contentEquals(com.example.popularmovies.Utils.Network.TOP_RATED))
setAdapter(movieData);
);
viewModel.getPopularMovies().observe(this, new Observer<List<com.example.popularmovies.Data.MovieData>>()
@Override
public void onChanged(@Nullable List<com.example.popularmovies.Data.MovieData> movieData)
Log.i("Test",""+ movieData);
if (movieData != null && mSortOrder.contentEquals(com.example.popularmovies.Utils.Network.POPULAR))
setAdapter(movieData);
);
任何建议如何做?
答案
使用LiveData,一个可观察的数据持有者类,在您的案例Activity Lifecycle中也具有生命周期意识。
声明变量
private MutableLiveData<String> toastMessageObserver = new MutableLiveData();
设置值
toastMessageObserver.setValue("Something unexpected happened to our request: "+response.message()); // Whenever you want to show toast use setValue.
Getter方法
在viewModel中定义getter方法
public LiveData<String> getToastObserver()
return toastMessageObserver;
setupViewModel内部的活动中
viewModel.getToastObserver().observe(this, message ->
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
);
以上是关于我想在ViewModel类的改装失败时显示Toast消息的主要内容,如果未能解决你的问题,请参考以下文章
Xcode 搜索控制器 - 在开始时显示搜索栏 (Swift 4)
我在颤动中的捕获错误没有按我想要的那样工作。我想在出现错误但未显示时显示错误消息