【问题标题】:Recyclerview not displaying data In Fragment using MVVMRecyclerview 不使用 MVVM 在 Fragment 中显示数据
【发布时间】:2021-03-09 13:12:22
【问题描述】:

我正在从 firebase 加载数据,我想使用 MVVM 在 recyclerview 中显示它 我从 firebase 检索数据,它工作正常。 但我想使用adapter.notifyDataSetChanged(); 更新 Repo 类中的 recyclerview 这是我的回购类:

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) {

            }

        });
    }
}

有没有办法使用 MVVM 更新 recyclerview?

【问题讨论】:

    标签: java mvvm android-recyclerview fragment


    【解决方案1】:

    LiveData 将在值更改时提供回调,即 setValuepostValue 。所以你需要在获取数据之后而不是之前设置值。

    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 thread 处理所有状态。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-24
      • 1970-01-01
      • 1970-01-01
      • 2020-08-21
      • 2019-01-09
      • 2016-06-21
      • 1970-01-01
      • 2019-07-11
      相关资源
      最近更新 更多