【发布时间】:2020-04-05 06:24:07
【问题描述】:
我正在开发一个游戏,当前用户可以在 RecyclerView 中查看他当前的所有游戏(见图)。为此,我正在使用 Firestore UI,并且我想将 TextView 更改为“播放”或“等待”,具体取决于当前用户必须玩还是对手必须玩。为此,我在 RecyclerViewAdapter 检索其值并为其提供当前玩家或对手的 UID 的值(取决于谁必须玩)的文档中创建了一个字段“转”。
所以当当前用户必须玩时,他应该在 RecyclerView 项目中看到“Play”作为 TextView,而对手必须看到“Wait”(见图)。为了获得所需的结果,我想实现一个 if-else 语句,但不幸的是 if 语句中的 textViewTurn.getText().toString() 没有值,但是当我将值作为意图发送到下一个 Activity 时它有一个值,所以我的 if else 语句是不工作。如何在CurrentGamesHolder 中获取此 TextView 的值?我应该在 RecyclerAdapter 的其他地方使用 if else 语句吗?
编辑:
我现在将 if else 语句移到 onBindViewHolder 中,其中 mTurn 和 uid 都有一个值,但该方法仍然不起作用。这意味着当我登录玩家 1 的帐户时,textViewPlay 显示“等待”,这与玩家 2 相同。uid 的值在两个帐户中是不同的,所以我实际上不知道如何两个帐户中的结果可能是相同的,因为根据 if else 语句它不应该是。
非常感谢任何帮助!
这是我的 RecyclerViewAdapter:
public class CurrentGameAdapter extends FirestoreRecyclerAdapter<CurrentGames, CurrentGameAdapter.CurrentGamesHolder> {
private OnItemClickListener listener;
public CurrentGameAdapter(@NonNull FirestoreRecyclerOptions<CurrentGames> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull CurrentGamesHolder holder, int position, @NonNull CurrentGames model) {
holder.textViewTurn.setText(model.getTurn());
holder.textViewPlay.setText("");
...
mTurn = model.getTurn();
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
String mCurrentUser =currentUser.getUid();
if (mTurn == mCurrentUser){
holder.textViewPlay.setText("Play");
}else if (mTurn != mCurrentUser){
holder.textViewPlay.setText("Wait");
}
}
@NonNull
@Override
public CurrentGamesHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listitem_current_game,parent, false);
return new CurrentGamesHolder(view);
}
class CurrentGamesHolder extends RecyclerView.ViewHolder {
TextView textViewTurn, textViewPlay;
...
public CurrentGamesHolder(@NonNull View itemView) {
super(itemView);
textViewPlay = itemView.findViewById(R.id.play_current_game);
textViewTurn = itemView.findViewById(R.id.turn_current_game);
textViewTurn.setVisibility(View.INVISIBLE);
...
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION && listener != null){
listener.onItemClick(getSnapshots().getSnapshot(position),(position));
Intent i = new Intent (itemView.getContext(), PlayInterimResultMainActivity.class);
i.putExtra("turn", textViewTurn.getText().toString());
itemView.getContext().startActivity(i);
}
}
});
}
}
public interface OnItemClickListener{
void onItemClick (DocumentSnapshot documentSnapshot, int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
this.listener = listener;
}
}
第一张图是玩家必须玩时在他的 RecyclerView 中看到的,第二张是对手看到的:
【问题讨论】:
标签: android firebase android-recyclerview google-cloud-firestore