0

I have question why my recyclerview is not updating data in real time when new was added? I getting data from firestore adding to list and passing to android view model class. Then i put in Live Data and passing to fragment. From fragment i sending to recyclerView Adapter. And later when i want to add new data it not displaying in recycler view. Maybe someone have solution to this problem?

This is my fragment where i getting data

        userViewModel.getDiscountList().observe(this, new Observer<List<DiscountModel>>() {
            @Override
            public void onChanged(@Nullable final List<DiscountModel> discountModels) {
                if (discountModels.size() != 0) {
                    if (adapter != null) {
                        discountAdapter.setData(discountModels);
                        dicountRecyclerView.scrollToPosition(0);
                    } else {
                        discountAdapter = new DiscountRecyclerViewAdapter(getContext(), discountModels);
                        dicountRecyclerView.setAdapter(discountAdapter);
                    }

                    dicountRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                        @Override
                        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                            super.onScrolled(recyclerView, dx, dy);
                            int firstItemVisible = layoutManager.findFirstVisibleItemPosition();
                            if (firstItemVisible != 0 && firstItemVisible % discountModels.size() == 0) {
                                recyclerView.getLayoutManager().scrollToPosition(0);
                            }
                        }
                    });
                }

            }
        });
    }

Here is my RecyclerView Adapter

    public void setData(List<DiscountModel> newData) {
        if (mDiscount != null) {
            PostDiffCallback postDiffCallback = new PostDiffCallback(mDiscount, newData);
            DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(postDiffCallback);

            mDiscount.clear();
            mDiscount.addAll(newData);
            diffResult.dispatchUpdatesTo(this);
        } else {
            // first initialization
            mDiscount = newData;
        }
    }

class PostDiffCallback extends DiffUtil.Callback {

        private final List<DiscountModel> oldPosts, newPosts;

        public PostDiffCallback(List<DiscountModel> oldPosts, List<DiscountModel> newPosts) {
            this.oldPosts = oldPosts;
            this.newPosts = newPosts;
        }

        @Override
        public int getOldListSize() {
            return oldPosts.size();
        }

        @Override
        public int getNewListSize() {
            return newPosts.size();
        }

        @Override
        public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
            return oldPosts.get(oldItemPosition).getDiscountId() == newPosts.get(newItemPosition).getDiscountId();
        }

        @Override
        public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
            return oldPosts.get(oldItemPosition).equals(newPosts.get(newItemPosition));
        }
    }

This is my Android View Model Class

private MutableLiveData<List<DiscountModel>> discountList;

    public LiveData<List<DiscountModel>> getDiscountList() {
        if (discountList == null) {
            discountList = new MutableLiveData<>();
            userRepository.getUserDiscounts(new MyFirebaseCallBack<List<DiscountModel>>() {
                @Override
                public void onSuccessCallback(List<DiscountModel> object) {
                    if (object != null) {
                        discountList.setValue(object);
                    }
                }

                @Override
                public void onFailureCallback(String message) {

                }
            });
        }
        return discountList;
    }


1 Answers1

0

I think we must notify the recycle view adapter to refresh it's view. Like mentioned on this answer https://stackoverflow.com/a/5092426/11351462