Java: How to make own total number counter for every RecyclerView item?

antsakontsa :

I need to make a total number counter for every RecyclerView item individually. Here are a picture and code to clear this situation out:

enter image description here

TTL is that total counter that I need. When I click player 1 plus symbol, total counter in that particular item should be increased by 1 and minus symbol decrease by 1.

What I have at the moment:

ViewHolder (handle minus and plus symbols):

public static class GameViewHolder extends RecyclerView.ViewHolder {
    public TextView mTextPlayer, mTextPar, mTotalTxt, mNumberTotal;
    public ImageView mImageMinus, mImagePlus;

    public GameViewHolder(@NonNull View itemView, final GameAdapter.OnItemClickListener listener) {
        super(itemView);
        mTextPlayer = itemView.findViewById(R.id.gameNameRecycler);
        mTextPar = itemView.findViewById(R.id.gameParNumberRecycler);
        mImageMinus = itemView.findViewById(R.id.game_minus_btn);
        mImagePlus = itemView.findViewById(R.id.game_plus_btn);
        mTotalTxt = itemView.findViewById(R.id.game_total_txt);
        mNumberTotal = itemView.findViewById(R.id.game_total_number);

        mImageMinus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    int position = getAdapterPosition();

                    if (position != RecyclerView.NO_POSITION) {
                        listener.onMinusClick(position);
                    }
                }
            }
        });

        mImagePlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    int position = getAdapterPosition();

                    if (position != RecyclerView.NO_POSITION) {
                        listener.onPlusClick(position);
                    }
                }
            }
        });
    }
}

In Activity:

mAdapter.setOnItemClickListener(new GameAdapter.OnItemClickListener() {
            int totalCounter = 0;

            @Override
            public void onMinusClick(int position) {
                String parNum = mGameItemList.get(position).getText2();
                int intParNm = Integer.valueOf(parNum);

                if (intParNm != 1) {
                    intParNm -= 1;
                    totalCounter -= 1;

                    mGameItemList.get(position).changeText2(Integer.toString(intParNm));
                    mGameItemList.get(position).changeText4(Integer.toString(totalCounter));
                    mAdapter.notifyDataSetChanged();
                }

            }

            @Override
            public void onPlusClick(int position) {
                String parNum = mGameItemList.get(position).getText2();
                int intParNm = Integer.valueOf(parNum);

                if (intParNm != 99) {
                    intParNm += 1;
                    totalCounter += 1;

                    mGameItemList.get(position).changeText2(Integer.toString(intParNm));
                    mGameItemList.get(position).changeText4(Integer.toString(totalCounter));
                    mAdapter.notifyDataSetChanged();
                }
            }
        });

So this is what I have now, but atm every item uses that same itemCounter, so if I click player 1 plus button, its TTL is 1 (as it should) but after that when I click player 2 plus button, player 2 TTL should be also 1 but is in fact 2, cause that 1 extra came from that player 1 plus click.

How I can change this to work for both individually?

Richard Dapice :

On thing is you can call notifyItemChanged(position), on the position of the row that has changed, it will only update that item in the recycler view.

Instead of holding your count in totalCounter, you could get the value from the viewholder that is already assigned, so something like String totalCounter = ((TextView) recyclerView.findViewHolderForAdapterPosition(position).itemView.findViewById(R.id.view)).getText().toString();

@Override
     public void onMinusClick(int position) {
            String parNum = mGameItemList.get(position).getText2();
            int intParNm = Integer.valueOf(parNum);

            if (intParNm != 1) {
                intParNm -= 1;
                totalCounter -= 1;
               mGameItemList.get(position).changeText2(Integer.toString(intParNm));
               mGameItemList.get(position).changeText4(Integer.toString(totalCounter));
               adapter.notifyItemChanged(position);
                }

            }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=126798&siteId=1