我有一个列表,我也添加了玩家,玩家名称基于列表的大小。当我最初设置播放器列表时,名称是正确的,但是当我向列表中添加新播放器时,名称不正确。请帮助以下是相关代码。
将玩家添加到列表中的方法:
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());
}
}
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);
}
}
}
列表中的玩家基数从两个开始,其名称显示为
玩家1和玩家2.但是当{1}传递到numberOfPlayers
addNewPlayers
时,玩家全部显示为玩家2。
addNewPlayer()
之前和之后的列表的屏幕截图。
答案 0 :(得分:0)
试试这个:覆盖适配器中的getItemCount以反映它正在使用的列表的大小,并调用该方法来获取列表中的项目数。
答案 1 :(得分:0)
问题在于我将新播放器添加到列表的开头而不是列表的末尾。说实话,我确定为什么会这样,但确实如此。新代码看起来像这样。
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());
}
}