The problem of failed to refresh data notifyDataSetChanged in RecyclerView of Android Development

It's very strange that notifyDataSetChanged can't refresh the data in any case when shopping cart is made today.

The code is as follows:

 marketCartTvEditShop.setOnClickListener(v -> {
            //Refresh the identification of the data, and edit the shopping cart
            isEditCart = !isEditCart;
            //Edit cart
            mCardAdapter.notifyDataSetChanged();
            Toast.makeText(getActivity(), "Click Edit", Toast.LENGTH_SHORT).show();
            if (isEditCart) {
                marketCartTvEditShop.setText("edit");
            } else {
                marketCartTvEditShop.setText("complete");
            }
        });

Take a look at the adapter

LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        marketCartRvData.setLayoutManager(layoutManager);
        //Set divider
        marketCartRvData.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayout.VERTICAL));
        marketCartRvData.setItemAnimator(new DefaultItemAnimator());
        mCardAdapter = new CardAdapter(cartShowData,isEditCart);
        marketCartRvData.setAdapter(mCardAdapter);
        initListener();

I don't think that's a problem

But notifyDataSetChanged doesn't refresh the data anyway. What's the matter?

Finally, Baidu knows that in the notifyDataSetChanged method, if the data passed in the adapter does not change, notifyDataSetChanged will not work, that is to say, the data in the incoming adapter can only be refreshed successfully if the data in the notifyDataSetChanged method is changed. So what do we do?

It is very simple to set a logo in the CNC center. For example, I set a Boolean as follows:

 /**
         * Can I edit the identification of shopping cart
         */
        private boolean editCart;

        public boolean isEditCart() {
            return editCart;
        }

        public void setEditCart(boolean editCart) {
            this.editCart = editCart;
        }

 

Let's look at the figure below. The code is not obvious.

 

That is to say, if I want to edit the shopping cart, I will say that editCart is set to true.

Which is the following code

 marketCartTvEditShop.setOnClickListener(v -> {
            isEditCart = !isEditCart;
            cartShowData.setEditCart(isEditCart);
            //Edit cart
            mCardAdapter.notifyDataSetChanged();
            Toast.makeText(getActivity(), "Click Edit", Toast.LENGTH_SHORT).show();
            if (isEditCart) {
                marketCartTvEditShop.setText("edit");
            } else {
                marketCartTvEditShop.setText("complete");
            }
        });

Better look at the picture

 

In this way, the data passed into the adapter will be changed, and notifyDataSetChanged can also refresh the data

Posted by idris on Thu, 21 Nov 2019 10:00:20 -0800