3

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?

1
  • try using notifyItemChanged(int position) Aug 1, 2019 at 6:34

1 Answer 1

1

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);
                }

            }
3
  • Won't work. My problem is not that if I click first items plus button, that it would change both player 1 and player 2 TTL number to the same number. My problem is that, they store their numbers into same variable, so I click player 1 plus button -> player 1 TTL: 1 AFTER THAT I click player 2 plus button -> player 2 TTL :2 but instead it should be 1 Aug 1, 2019 at 6:44
  • They both should have their own variable where to store their own individual TTL number Aug 1, 2019 at 6:46
  • Awesome. I didn't have a ton of time to write up a more complete explanation. But I am glad you were able to figure it out! Aug 2, 2019 at 3:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.