英文:
I want to change the status of JOIN button to "JOINED" when string is matched from the list of strings retrieved from firestore?
问题
我试图将"Join"按钮的文本设置为"Joined",如果从列表中匹配到字符串。问题是,"else"循环也在执行,这是我不想要的。
以下是您的代码中已翻译的部分:
public void JoinBtnState(ContestModel model, ContestViewHolder holder, long totalSlot) {
String currUser = mAuth.getCurrentUser().getUid();
String MatchID = model.getMatchID();
CollectionReference collectionReference = fStore.collection("users").document(currUser).collection("ContestJoined");
// 获取比赛列表和比赛列表
collectionReference.addSnapshotListener((value, error) -> {
if (!value.isEmpty()) {
DocumentSnapshot documentSnapshot;
List<DocumentSnapshot> MatchList = value.getDocuments();
for (int i = 0; i < MatchList.size(); i++) {
documentSnapshot = MatchList.get(i);
String MatchListID = documentSnapshot.getId();
if (MatchID.equals(MatchListID)) {
System.out.println("FOUND MATCH !" + MatchID + "====" + MatchListID);
holder.joinButton.setText("JOINED");
holder.joinButton.setEnabled(false);
break;
} else {
System.out.println("ELSE METHOD CALLED !" + MatchID);
holder.joinButton.setText("JOIN");
holder.joinButton.setEnabled(true);
}
}
} else {
holder.joinButton.setText("JOIN");
holder.joinButton.setEnabled(true);
System.out.println("EMPTY 1 !");
}
});
}
请注意,我已经将HTML编码转换为正常的引号,并将代码中的注释进行了适当的处理。
英文:
I am trying to set the Join Button text to Joined if i get the Strings Matched from List. The problem is else loop is also getting executed which i don't want.
Here is my code
>Java
public void JoinBtnState(ContestModel model, ContestViewHolder holder, long totalSlot) {
String currUser = mAuth.getCurrentUser().getUid();
String MatchID = model.getMatchID();
CollectionReference collectionReference = fStore.collection("users").document(currUser).collection("ContestJoined");
//fetching matchlist and contest list
collectionReference.addSnapshotListener((value, error) -> {
if (!value.isEmpty()) {
DocumentSnapshot documentSnapshot;
List<DocumentSnapshot> MatchList = value.getDocuments();
for (int i = 0; i < MatchList.size(); i++) {
documentSnapshot = MatchList.get(i);
String MatchListID = documentSnapshot.getId();
if (MatchID.equals(MatchListID)) {
System.out.println("FOUND MATCH !"+MatchID+"===="+MatchListID);
holder.joinButton.setText("JOINED");
holder.joinButton.setEnabled(false);
break;
} else {
System.out.println("ELSE METHOD CALLED !"+MatchID);
holder.joinButton.setText("JOIN");
holder.joinButton.setEnabled(true);
}
}
} else {
holder.joinButton.setText("JOIN");
holder.joinButton.setEnabled(true);
System.out.println("EMPTY 1 !");
}
});
}
答案1
得分: 0
如果我理解正确,您想要:
- 检查某个值是否存在于列表中。
- 根据检查结果设置一个UI元素。
在这种情况下,最容易的方法是将检查和UI更新分开。类似这样的代码:
DocumentSnapshot documentSnapshot;
List<DocumentSnapshot> MatchList = value.getDocuments();
boolean isInList = false;
for (int i = 0; i < MatchList.size(); i++) {
documentSnapshot = MatchList.get(i);
String MatchListID = documentSnapshot.getId();
if (MatchID.equals(MatchListID)) {
isInList = true;
}
}
if (isInList) {
System.out.println("FOUND MATCH !"+MatchID+"===="+MatchListID);
holder.joinButton.setText("JOINED");
holder.joinButton.setEnabled(false);
} else {
System.out.println("ELSE METHOD CALLED !"+MatchID);
holder.joinButton.setText("JOIN");
holder.joinButton.setEnabled(true);
}
与此无关的是:我觉得使用Firebase文档上关于监听集合中多个文档的代码更易读:
boolean isInList = false;
for (QueryDocumentSnapshot doc : value) {
if (MatchID.equals(doc.getId())) {
isInList = true;
}
}
...
英文:
If I understand correctly you want to:
- Check if a certain value exists in a list.
- Set a UI element based on the result of that.
In that case it's easiest to separate the check and the UI update. Something like this:
DocumentSnapshot documentSnapshot;
List<DocumentSnapshot> MatchList = value.getDocuments();
bool isInList = false;
for (int i = 0; i < MatchList.size(); i++) {
documentSnapshot = MatchList.get(i);
String MatchListID = documentSnapshot.getId();
if (MatchID.equals(MatchListID)) {
isInList = true;
}
}
if (isInList) {
System.out.println("FOUND MATCH !"+MatchID+"===="+MatchListID);
holder.joinButton.setText("JOINED");
holder.joinButton.setEnabled(false);
} else {
System.out.println("ELSE METHOD CALLED !"+MatchID);
holder.joinButton.setText("JOIN");
holder.joinButton.setEnabled(true);
}
Unrelated to that: I find it a bit more readable to loop over the results of a query by using the code from the Firebase documentation on listening to multiple documents in a collection:
bool isInList = false;
for (QueryDocumentSnapshot doc : value) {
if (MatchID.equals(doc.getId()) {
isInList = true;
}
}
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论