我有一个简单的应用程序,它轮询服务以在recyclerview中显示国家/地区列表。在这里,只要国家/地区列表中有任何更改,我就会使用LiveData来更新recyclerview。麻烦的是,LiveData的onChanged方法仅在第一次调用setValue时被调用。但是之后如果对数据有任何进一步的改变,就不会调用onChanged ??
以下是我的代码以获取更多信息 -
//Setup observer
Observer<List<CountryModel>> myObserver = new Observer<List<CountryModel>>() {
@Override
public void onChanged(@Nullable List<CountryModel> countryModels) {
mCountryList = countryModels;
//Update recyclerview
myAdapter.updateRecyclerView(mCountryList);
}
};
//Set Observer for Viewmodel
countryViewModel.getmCountryList().observe(this,myObserver);
公共类CountryViewModel扩展AndroidViewModel { private MutableLiveData&gt; mCountryList; private MyRetrofitClient myRetrofitClient;
public CountryViewModel(@NonNull Application application) {
super(application);
}
public void init(){
mCountryList = new MutableLiveData<>();
myRetrofitClient = new MyRetrofitClient();
**mCountryList = myRetrofitClient.getCountryList(); //This works**
pollCountryList();
}
//Create polling request for every 10 secs
private void pollCountryList(){
final Handler mHandler = new Handler();
new Thread(new Runnable() {
@Override
public void run() {
for (int i=0; i<30;i++){
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//Call API on main thread
mHandler.post(new Runnable() {
@Override
public void run() {
**myRetrofitClient.getCountryList(); //NOT CALLING ONCHANGED??**
}
});
}
}
}).start();
}
public MutableLiveData<List<CountryModel>> getmCountryList() {
return mCountryList;
}
public MutableLiveData<List<CountryModel>> getCountryList(){ final MutableLiveData<List<CountryModel>> lstResult = new MutableLiveData<>(); MockServiceInterface serviceInterface = mRetrofit.create(MockServiceInterface.class); Call<List<CountryModel>> countryList = serviceInterface.getCountryList(); countryList.enqueue(new Callback<List<CountryModel>>() { @Override public void onResponse(Call<List<CountryModel>> call, Response<List<CountryModel>> response) { if (response.isSuccessful()){ List<CountryModel> lstResponse = response.body(); lstResult.setValue(lstResponse); }else { System.out.print(response.errorBody()); } }
@Override
public void onFailure(Call<List<CountryModel>> call, Throwable t) {
t.printStackTrace();
}
});
return lstResult;
}
谢谢!
修改
一些额外的意见 - 当我在CountryViewModel中调用MutableLiveData实例(mCountryList)的setValue方法时,它每次调用onChanged方法。
然而,在MyRetrofitClient的情况下它有所不同。第一次在MyRetrofitClient.getCountryList()中调用setValue时,它会调用onChanged方法。但后来却没有。
答案 0 :(得分:0)
抱歉,我一开始误解了问题。
您没有收到更改,因为您从未在setValue
上致电mCountryList
。
方法getCountryList()
正在返回新对象MutableLiveData<List<CountryModel>> lstResult
所有被调用的对象,没有人在观察。
<强>解决方案:强>
不要使用MutableLiveData
返回getCountryList
个对象,而是在mCountryList
中设置onResponse()
。
<强>代码强>
public void init(){
mCountryList = new MutableLiveData<>();
myRetrofitClient = new MyRetrofitClient();
myRetrofitClient.getCountryList();
pollCountryList();
}
public LiveData<List<CountryModel>> getCountryListener() {
return mCountryList;
}
public void getCountryList(){
MockServiceInterface serviceInterface = mRetrofit.create(MockServiceInterface.class);
Call<List<CountryModel>> countryList = serviceInterface.getCountryList();
countryList.enqueue(new Callback<List<CountryModel>>() {
@Override
public void onResponse(Call<List<CountryModel>> call, Response<List<CountryModel>> response) {
if (response.isSuccessful()){
List<CountryModel> lstResponse = response.body();
mCountryList.setValue(lstResponse);
}else {
System.out.print(response.errorBody());
}
}
@Override
public void onFailure(Call<List<CountryModel>> call, Throwable t) {
t.printStackTrace();
}
});
}
使用getCountryListener()
进行观察。
<强>活动:强>
countryViewModel.getCountryListener().observe(this,myObserver);