Android 仅第一次调用LiveData onChanged方法
我有一个简单的应用程序,可以通过轮询服务在recyclerview中显示国家列表。在这里,每当国家/地区列表发生任何变化时,我都会使用LiveData更新recyclerview。问题是,LiveData的onChanged方法只有在第一次调用setValue时才被调用。但在此之后,如果对数据有任何进一步的更改,则不会调用onChanged 以下是我的代码以获取更多信息- CountryListFragmentAndroid 仅第一次调用LiveData onChanged方法,android,retrofit2,android-livedata,Android,Retrofit2,Android Livedata,我有一个简单的应用程序,可以通过轮询服务在recyclerview中显示国家列表。在这里,每当国家/地区列表发生任何变化时,我都会使用LiveData更新recyclerview。问题是,LiveData的onChanged方法只有在第一次调用setValue时才被调用。但在此之后,如果对数据有任何进一步的更改,则不会调用onChanged 以下是我的代码以获取更多信息- CountryListFragment //设置观察者 观察者myObserver=新观察者(){ @凌驾 更改后的公共vo
//设置观察者
观察者myObserver=新观察者(){
@凌驾
更改后的公共void(@Nullable List countryModels){
McCountryList=countryModels;
//更新回收视图
myAdapter.updateRecyclerView(mCountryList);
}
};
//为Viewmodel设置观察者
countryViewModel.GetMcCountryList().observe(这是myObserver);
CountryViewModel
公共类CountryViewModel扩展了AndroidViewModel{
私有MutableLiveData>mCountryList;
私人MyRefughtClient MyRefughtClient
公共CountryViewModel(@NonNull应用程序){
超级(应用);
}
公共void init(){
mCountryList=新的可变LiveData();
MyRefughtClient=新建MyRefughtClient();
**mCountryList=myclient.getCountryList();//这很有效**
pollcontrylist();
}
//每10秒创建一次轮询请求
私有void pollconrylist(){
最终处理程序mHandler=新处理程序();
新线程(newrunnable()){
@凌驾
公开募捐{
对于(inti=0;对不起,我一开始误解了这个问题 您没有收到更改,因为您从未在
mCountryList
上调用setValue
方法getCountryList()
正在返回新对象MutableLiveData lstResult
调用的所有对象,但没有人观察到这些对象
解决方案:
countryViewModel.getCountryListener().observe(this,myObserver);
在onResponse()
中设置mCountryList
,而不是使用getCountryList
返回MutableLiveData
对象
代码
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()是的,你是对的。但是我没有在getCountryList方法中分配mCountryList,而是将MutableLiveData lstResult的初始化代码移动到MyClient类的init方法。因此,在初始化期间只创建了一个MutableLiveData实例。这将分配给mCountryList,以后每次它的设置值为s调用,相应的onChanged也被调用。谢谢!
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;
}
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();
}
});
}
countryViewModel.getCountryListener().observe(this,myObserver);