Java: How to make RecyclerView item number change to the same number in Activity? (real time)

antsakontsa :

As the title says, I need to make RecyclerView item number, change to that same number, which is in that same Activity (in real-time). So when the number in Activity changes, also that number which is in the RecyclerView item, should change to that same number.

enter image description here

When the game starts, par number and those numbers in RecyclerView are the same as they should but ...

enter image description here

Now the 2nd hole par number changes to 3, but those RecyclerView item numbers are still 4, but instead, they should be also 3. How Can I make this change happen?

Here is my adapter:

@Override
    public void onBindViewHolder(@NonNull GameViewHolder holder, int position) {
        GameItem currentItem = mGameList.get(position);

        holder.mTextPlayer.setText(currentItem.getText1());
        holder.mTextPar.setText(currentItem.getText2());

        /** If persons par number is smaller than course par number, then change persons par number background to blue **/
        if (Integer.parseInt(holder.mTextPar.getText().toString()) == 1) {
            holder.mTextPar.setBackgroundResource(R.drawable.border_box_yellow);
        } else if (Integer.parseInt(holder.mTextPar.getText().toString()) < Integer.parseInt(ActivityGame.mParNm.getText().toString())) {
            holder.mTextPar.setBackgroundResource(R.drawable.border_box_blue);
        } else if (Integer.parseInt(holder.mTextPar.getText().toString()) > Integer.parseInt(ActivityGame.mParNm.getText().toString())) {
            holder.mTextPar.setBackgroundResource(R.drawable.border_box_red);
        } else if (Integer.parseInt(holder.mTextPar.getText().toString()) == Integer.parseInt(ActivityGame.mParNm.getText().toString())) {
            holder.mTextPar.setBackgroundResource(R.drawable.border_box_green);
        }

Here in my GameAdapter, where I set those items background colors (according also to that par number) I tried to do:

holder.mTextPar.setText(ActivityGame.mParNm.getText().toString());

Meaning that I tried to set the number according to Par number, but it didn't change at all, it just changed the background of the items, but number stayed the same.

Hari N Jha :

Do two simple things.

Create a method in the adapter-

public void updateAdapterData() {
    notifyDataSetChanged();
}

Next, add the below method in the activity and call it when there is a change in the par value in your activity.

private void updateGameArrayList(int parVal) {
    for(GameItem model : GameList) {
        model.setText2(parVal);
        adapterVariableInActivity.updateAdapterData();
    }
}

Guess you like

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