简体   繁体   中英

adapter.notifyItemChanged(position) has no effect

For each item of the list ("itemList")

ArrayList<Item> itemList = new ArrayList<>();

shown by "recyclerView" that is controlled by "itemAdapter", its quantity is displayed, as well as a plus and minus button which allows the user to respectively increase and decrease the quantity.

In the set-up of "itemAdapter" these buttons are given functionality by:

itemAdapter.setOnItemClickListener(new ItemAdapter.OnItemClickListener() {

    @Override
    public void onPlusClick(int position) {
        itemList.get(position).setQuantity(itemList.get(position).getQuantity() + 1);
        itemAdapter.notifyItemChanged(position);

        Log.d(TAG, "onPlusClick: position = " + position + " quantity = " + itemList.get(position).getQuantity());


    }

    @Override
    public void onMinusClick(int position) {
        itemList.get(position).setQuantity(itemList.get(position).getQuantity() - 1);
        if (itemList.get(position).getQuantity() < 1) {
            itemList.get(position).setQuantity(1);
        }
        itemAdapter.notifyItemChanged(position);

        Log.d(TAG, "onMinusClick: position = " + position + " quantity = " + itemList.get(position).getQuantity());

    }
})

The display of the quantity is set up in the onBindViewHolder() of the adapter class ItemAdapter with:

holder.textviewQuantity.setText(String.valueOf(currentItem.getQuantity()));

And this is the code for the adapter class

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemListHolder> {

    private ArrayList<Item> mItemList;
    private OnItemClickListener mListener;

    public interface OnItemClickListener {

        void onPlusClick(int position);    
        void onMinusClick(int position);  

    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    @NonNull
    @Override
    public ItemListHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
        return new ItemListHolder(v, mListener);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemListHolder holder, int position) {

        Item currentItem = mItemList.get(position);    
        holder.imageviewCategoryIcon.setImageResource(Main.getIconID(currentItem.getCategoryNumber()));
        holder.textviewItemName.setText(currentItem.getItemName());
        holder.textviewQuantity.setText(String.valueOf(currentItem.getQuantity()));    

        }    
       
    }

    @Override
    public int getItemCount() {

        return mItemList.size();
    }

    public static class ItemListHolder extends RecyclerView.ViewHolder {

        CardView cardviewItem;
        ImageView imageviewCategoryIcon;
        TextView textviewItemName;
        Button buttonPlus;
        Button buttonMinus;
        TextView textviewQuantity;          

        public ItemListHolder(@NonNull View view, OnItemClickListener listener) {
            super(view);

            cardviewItem = view.findViewById(R.id.cardview_item);
            imageviewCategoryIcon = view.findViewById(R.id.imageview_category_icon);
            textviewItemName = view.findViewById(R.id.textview_item_name);
            buttonPlus = view.findViewById(R.id.button_plus);
            buttonMinus = view.findViewById(R.id.button_minus);                

            buttonPlus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        int position = getBindingAdapterPosition();
                        if (position != RecyclerView.NO_POSITION) {
                            listener.onPlusClick(position);
                        }
                    }
                }
            });
            buttonMinus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        int position = getBindingAdapterPosition();
                        if (position != RecyclerView.NO_POSITION) {
                            listener.onMinusClick(position);
                        }
                    }
                }
            });              
        }
    }

    public ItemAdapter(ArrayList<Item> itemList) {
        mItemList = itemList;
    }

}

When the app starts up, the list of items is displayed, with each item showing the correct quantity.

But when the user clicks a plus or minus button, the quantity displayed doesn't change , though the logs clearly indicate the correct position in "itemList" has been chosen, and that the quantity of the corresponding item has indeed changed correctly.

It looks like itemAdapter.notifyItemChanged(position) has no effect. I also tried itemAdapter.notifyDataSetChanged() , but with no effect either. The recycler display is not being refreshed with the data changes.

What could be wrong?

PS: my very unworkable workaround is to run the whole recycler set-up each time that plus or minus button is clicked. Works fine if the list of items is no longer than the screen, but of course, resets to the top of the list if you press a button of an item that was found scrolling down.

I'm the author of the question.

Amazingly, itemAdapter.notify... not working seems to be connected to the app using FireStore db .

"itemList" is stored to / retrieved from FireStore db . When I replace this with "itemList" being stored to / retrieved from SharedPreferences , the itemAdapter.notify... problem disappears (??)

If anyone has an explanation of how this could be possible, please post.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM