0

I have a collection of products and I want to get realtime updates. This is my code:

query.addSnapshotListener(new EventListener<QuerySnapshot>() {
    @Override
    public void onEvent(@Nullable QuerySnapshot querySnapshot, @Nullable FirebaseFirestoreException e) {
        if (e != null) return;

        List<Product> list = new ArrayList<>();
        for (DocumentChange documentChange : querySnapshot.getDocumentChanges()) {
            switch (documentChange.getType()) {
                case ADDED:
                    Product product = documentChange.getDocument().toObject(Product.class);
                    list.add(product);
                    break;
                case MODIFIED:
                    adapter.notifyDataSetChanged();
                    break;
                case REMOVED:
                    //
                    break;
            }
        }
        adapter = new ProductAdapter(context, list);
        recyclerView.setAdapter(adapter);
    }
});

If the price of a product changes and I have the app in foreground, I cannot see the change in my RecyclerView at all. I still see the old price even if I notify the adapter. There is also something weird happening. If the price of tenth product changes, the scroll goes to first position.

How can I see the realtime change when a price is changed? And how can remain at the current position in the RecyclerView when a change occurs?

Edit:

case MODIFIED:
    Product modifiedProduct = documentChange.getDocument().toObject(Product.class);
    for(Product p : list) {
        if(p.getProductName().equals(modifiedProduct.getProductName())) {
            list.remove(p);
        }
    }
    list.add(modifiedProduct);
    adapter.notifyDataSetChanged();

The same thing happens. Still seeing the old price.

Oleg Caralanski
  • 330
  • 2
  • 10

1 Answers1

1

You're not putting the updated product data into your list yet.

case MODIFIED:
    Product product = documentChange.getDocument().toObject(Product.class);
    // TODO: Replace the existing product in the list with the updated one
    adapter.notifyDataSetChanged();
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank you for the answer but it doesn't work. Please see my edited question. – Oleg Caralanski Feb 06 '19 at 19:12
  • "it doesn't work" is not very helpful. **What** specifically doesn't work about that code? Did you step through it in a debugger? Does it find the product and remove it? Does the new product you add (`list.add(modifiedProduct)`) have the new properties (when checked in the debugger)? – Frank van Puffelen Feb 06 '19 at 19:16
  • Yes, you're right, I debug the code and now it works thanks to you. Thank you. Do you know by chance why is scrolling to the first position when a price is changed and how can I avoid this? – Oleg Caralanski Feb 06 '19 at 20:33
  • I don't know. But I'd wager a search should give some relevant answer. For example, this looks promising: https://stackoverflow.com/questions/28658579/refreshing-data-in-recyclerview-and-keeping-its-scroll-position – Frank van Puffelen Feb 06 '19 at 21:37