0

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. But I want to use adapter.notifyDataSetChanged(); to update recyclerview in Repo class this is my 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?

FadyFouad
  • 3
  • 3

1 Answers1

0

LiveData will provide callback on value change i.e setValue or 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 . Go through This thread to handle all the states.

ADM
  • 16,256
  • 11
  • 37
  • 69