My textView is not receiving the correct value from list.size(), why not?

I have a list that I add Players too and the Player names are based off the size of the list. When I initially set the player list the names are correct but when I add a new Player to the list the name is incorrect. Please help below is the relevant code.

The method that adds the Players to the List:

public static void addNewPlayers(List<Player> playerArrayList, SharedPreferences sharedPreferences,
                                 PlayerScoreCardAdapter playerScoreCardAdapter,
                                 int numberOfPlayersToAdd) {

    for (int i = 0; i < numberOfPlayersToAdd; i++) {

        // TODO: 2016-03-19 Fix player name not updating
        String name = String.format("%s%s",
                Keys.KEY_DEFAULT_PLAYER_NAME, playerArrayList.size() + 1);

        Player player = new Player(name,
                sharedPreferences.getInt(Keys.KEY_PLAYER_SCORE + String.valueOf(i + 1),
                        Keys.KEY_DEFAULT_PLAYER_SCORE));

        playerArrayList.add(i, player);
        playerScoreCardAdapter.notifyItemInserted(playerArrayList.size());
    }
}

The RecylcerView.Adapter:

public class PlayerScoreCardAdapter extends RecyclerView.Adapter<PlayerScoreCardAdapter.ScoreViewHolder> {

private List<Player> mPlayerList;

public PlayerScoreCardAdapter(List<Player> playerList) {
    mPlayerList = playerList;

}

@Override
public ScoreViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.player_score_list_item, parent, false);

    return new ScoreViewHolder(view);
}

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

    holder.bindPlayers(mPlayerList.get(position));

}

@Override
public int getItemCount() {
    return mPlayerList.size();
}

public class ScoreViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

    public TextView mPlayerName;
    public EditText mPlayerScore;
    public Button mMinusButton, mAddButton;

    public ScoreViewHolder(View itemView) {
        super(itemView);

        mPlayerName = (TextView) itemView.findViewById(R.id.textView_player_name);
        mPlayerScore = (EditText) itemView.findViewById(R.id.editText_player_score);
        mMinusButton = (Button) itemView.findViewById(R.id.button_minus);
        mAddButton = (Button) itemView.findViewById(R.id.button_add);

        mMinusButton.setOnClickListener(this);
        mAddButton.setOnClickListener(this);

        mPlayerName.setOnLongClickListener(this);


    }

    /**
     * Binds the data from the player to the appropriate views.
     *
     * @param player The player data to be bound.
     */
    public void bindPlayers(Player player) {

        mPlayerName.setText(player.getName());
        mPlayerScore.setText(String.valueOf(player.getScore()));

    }

    @Override
    public void onClick(View v) {

        int id = v.getId();
        int updatedScoreCount = mPlayerList.get(getAdapterPosition()).getScore();

        switch (id) {
            case R.id.button_minus:
                updatedScoreCount--;
                if (updatedScoreCount < 0) {
                    Toast.makeText(itemView.getContext(), R.string.toast_text_score_to_low, Toast.LENGTH_LONG).show();
                    return;
                } else {
                    updatePlayerScore(updatedScoreCount);
                    notifyItemChanged(getAdapterPosition());
                }
                break;
            case R.id.button_add:
                updatedScoreCount++;
                if (updatedScoreCount >= 9999) {
                    Toast.makeText(itemView.getContext(), R.string.toast_text_score_to_high, Toast.LENGTH_LONG).show();
                    return;
                } else {
                    updatePlayerScore(updatedScoreCount);
                    notifyItemChanged(getAdapterPosition());
                }
                break;
            default:
        }

    }

    @Override
    public boolean onLongClick(View v) {
        removePlayer(getAdapterPosition());
        return true;
    }

    public void removePlayer(int positionInArrayList) {
        mPlayerList.remove(positionInArrayList);
        notifyItemRemoved(positionInArrayList);

    }

    private void updatePlayerScore(int updatedScoreCount) {
        mPlayerList.get(getAdapterPosition()).setScore(updatedScoreCount);
    }
}

}

The base number of Players in the list starts as two and their names show up as Player 1 and Player 2. But when 1 is passed into numberOfPlayers in addNewPlayers the players all show up as Player 2.

Screen captures of the list before and after addNewPlayer().

Answers


try this: override the getItemCount in the adapter to reflect the size of the list it is using, and call that method instead to get the number of items in your list.


The problem was that I was adding the new Player to the beginning of the list and not the end of the list. To be honest I'm sure why this worked but it does. the new code looks like this.

public static void addNewPlayers(List<Player> playerArrayList, SharedPreferences sharedPreferences,
                                 PlayerScoreCardAdapter playerScoreCardAdapter,
                                 int numberOfPlayersToAdd) {

    for (int i = 0; i < numberOfPlayersToAdd; i++) {

        String name = String.format("%s%s", Keys.KEY_DEFAULT_PLAYER_NAME, playerArrayList.size() + 1);

        Player player = new Player(name, sharedPreferences.getInt(Keys.KEY_PLAYER_SCORE + String.valueOf(i + 1),
                Keys.KEY_DEFAULT_PLAYER_SCORE));

        playerArrayList.add(playerArrayList.size(), player);
        playerScoreCardAdapter.notifyItemInserted(playerArrayList.size());
    }
}

Need Your Help

How to make data available to all objects of a class?

c++ class oop field

This is probably very basic but somehow I cannot figure it out.

Get the parent of a node in Data.Tree (haskell)

haskell tree

I need a tree implementation where I can access the parent node of any node in the tree. Looking at Data.Tree I see the tree definition: