[英]Recyclerview not displaying data In Fragment using MVVM
I'm loading data From firebase and I want to display it in recyclerview using MVVM I retrieved data from firebase and it works fine.我正在从 firebase 加载数据,我想使用 MVVM 在 recyclerview 中显示它我从 firebase 检索数据,它工作正常。 But I want to use
adapter.notifyDataSetChanged();
但我想使用
adapter.notifyDataSetChanged();
to update recyclerview in Repo class this is my repo class:在 Repo class 中更新 recyclerview 这是我的 repo class:
public class CategoriesRepo {
private static CategoriesRepo instance;
private final ArrayList<Cat> categoriesModel = new ArrayList<>();
private DatabaseReference dbCategories;
public static CategoriesRepo getInstance() {
if (instance == null) {
instance = new CategoriesRepo();
}
return instance;
}
public MutableLiveData<ArrayList<Cat>> getCategories() {
loadCats();
MutableLiveData<ArrayList<Cat>> categories = new MutableLiveData<>();
categories.setValue(categoriesModel);
return categories;
}
private void loadCats() {
dbCategories = FirebaseDatabase.getInstance().getReference("categories");
dbCategories.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NotNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String name = ds.getKey();
// this is not showing in recyclerview
categoriesModel.add(new Cat("Name", 1));
Log.d("TAGD", "onDataChange: " + ds.getKey() + " " + categoriesModel.size());
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
Is there any way to update recyclerview using MVVM?有没有办法使用 MVVM 更新 recyclerview?
LiveData
will provide callback on value change ie setValue
or postValue
. LiveData
将在值更改时提供回调,即setValue
或postValue
。 So you need to set the value after you get the data not before.因此,您需要在获取数据之后而不是之前设置值。
public class CategoriesRepo {
private static CategoriesRepo instance;
private DatabaseReference dbCategories;
private MutableLiveData<ArrayList<Cat>> categories = new MutableLiveData<>();
public static CategoriesRepo getInstance() {
if (instance == null) {
instance = new CategoriesRepo();
}
return instance;
}
public MutableLiveData<ArrayList<Cat>> getCategories() {
loadCats();
return categories;
}
private void loadCats() {
dbCategories = FirebaseDatabase.getInstance().getReference("categories");
dbCategories.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NotNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
ArrayList<Cat> categoriesModel = new ArrayList<>()
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String name = ds.getKey();
categoriesModel.add(new Cat("Name", 1));
}
categories.setValue(categoriesModel);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
This should work.这应该有效。 Also you have to handle Error state with data loading.
此外,您还必须处理带有数据加载的错误 state。 Go through This thread to handle all the states.
Go 通过这个线程来处理所有的状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.