首页 > 解决方案 > Recyclerview 不使用 MVVM 在 Fragment 中显示数据

问题描述

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

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?

标签: javamvvmandroid-recyclerviewfragment

解决方案


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

            }

        });
    }
}

这应该工作。您还必须通过数据加载处理错误状态。通过这个线程来处理所有的状态。


推荐阅读