I want to change the status of JOIN button to "JOINED" when string is matched from the list of strings retrieved from firestore?

huangapple go评论89阅读模式
英文:

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(&quot;users&quot;).document(currUser).collection(&quot;ContestJoined&quot;);
//fetching matchlist and contest list
collectionReference.addSnapshotListener((value, error) -&gt; {
if (!value.isEmpty()) {
DocumentSnapshot documentSnapshot;
List&lt;DocumentSnapshot&gt; MatchList = value.getDocuments();
for (int i = 0; i &lt; MatchList.size(); i++) {
documentSnapshot = MatchList.get(i);
String MatchListID = documentSnapshot.getId();
if (MatchID.equals(MatchListID)) {
System.out.println(&quot;FOUND MATCH !&quot;+MatchID+&quot;====&quot;+MatchListID);
holder.joinButton.setText(&quot;JOINED&quot;);
holder.joinButton.setEnabled(false);
break;
} else {
System.out.println(&quot;ELSE METHOD CALLED !&quot;+MatchID);
holder.joinButton.setText(&quot;JOIN&quot;);
holder.joinButton.setEnabled(true);
}
}
} else {
holder.joinButton.setText(&quot;JOIN&quot;);
holder.joinButton.setEnabled(true);
System.out.println(&quot;EMPTY 1 !&quot;);
}
});
}

答案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&lt;DocumentSnapshot&gt; MatchList = value.getDocuments();
bool isInList = false;
for (int i = 0; i &lt; MatchList.size(); i++) {
documentSnapshot = MatchList.get(i);
String MatchListID = documentSnapshot.getId();
if (MatchID.equals(MatchListID)) {
isInList = true;
}
}
if (isInList) {
System.out.println(&quot;FOUND MATCH !&quot;+MatchID+&quot;====&quot;+MatchListID);
holder.joinButton.setText(&quot;JOINED&quot;);
holder.joinButton.setEnabled(false);
} else {
System.out.println(&quot;ELSE METHOD CALLED !&quot;+MatchID);
holder.joinButton.setText(&quot;JOIN&quot;);
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;
}
}
...

huangapple
  • 本文由 发表于 2020年7月31日 02:11:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63178955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定