BVB09
BVB09

Reputation: 875

Firestore snapshot listener: Recyclerview not being updated

I am not sure what I am doing wrong here. If I pull to refresh (not shown in the code below) I retrieve the changes. But if the document is modified somehow and the UI on the client is not altered, the recyclerview items which show the data remain unaltered.

private ListenerRegistration registration;

private void getMealplans() {
        String userId = (bundle != null && bundle.containsKey("userId")) ? bundle.getString("userId") : "";
        Query mealplanQuery = firebaseFirestore.collection("Meal_Plans").whereEqualTo("userId", userId).orderBy("timeOfCreation", Query.Direction.DESCENDING);

registration = mealplanQuery.addSnapshotListener((value, error) -> {
            if (error == null && value != null) {
                for (QueryDocumentSnapshot document : value) {
                    Mealplan mealplan = document.toObject(Mealplan.class).withId(document.getId());
                    mealplanArrayList.add(updatedmealplan);
                }

                String planId = (bundle != null && bundle.containsKey("planId")) ? bundle.getString("planId") : "none";
                mealPlanAdapter = new MealPlanAdapter(mealplanContext, mealplanArrayList, planId);
                mealplanRecycler.setAdapter(mealPlanAdapter);
                mealPlanAdapter.setEditMealplanListener(this::onButtonPressed);

            } else {
                Log.d(TAG, "error is not null: " + Objects.requireNonNull(error).getLocalizedMessage());
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!InternetConnection.checkConnection(mealplanContext)) {
            new ShowSnackbar(mealplanView, mealplanContext);
        }
        getMealplans();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        createPlanListener = null;
    }

Upvotes: 0

Views: 310

Answers (2)

iamanbansal
iamanbansal

Reputation: 2732

As I commented above

registration = mealplanQuery.addSnapshotListener((value, error) -> {
                if (error == null && value != null) {
                    mealplanArrayList.clear // clear the items in existing list
                    for (QueryDocumentSnapshot document : value) {
                        Mealplan mealplan = document.toObject(Mealplan.class).withId(document.getId());
                        //add new items here
                        mealplanArrayList.add(updatedmealplan);
                    }

            String planId = (bundle != null && bundle.containsKey("planId")) ? bundle.getString("planId") : "none";
            mealPlanAdapter = new MealPlanAdapter(mealplanContext, mealplanArrayList, planId);
            mealplanRecycler.setAdapter(mealPlanAdapter);
            mealPlanAdapter.setEditMealplanListener(this::onButtonPressed);

        } else {
            Log.d(TAG, "error is not null: " + Objects.requireNonNull(error).getLocalizedMessage());
        }
    });

Upvotes: 2

Puteri
Puteri

Reputation: 3789

I'll add the suggestion of @iamanbansal in the comments for better visibility of the answer.

As @iamanbansal mentioned:

You're adding the items in the existing list that's why it's not updating the recycleview. You can see those items in the end of rv. You should create new list for new items and pass in the adapter.

Upvotes: 0

Related Questions