繁体   English   中英

为什么我的 else 块没有在 java android 工作室的 OnClickListener 中执行

[英]Why is my else block not executing in my OnClickListener in java android studio

我创建了一个功能,让我的应用程序用户最喜欢的商店。 现在,如果用户已经收藏了一家商店并再次单击该按钮,它应该从 Firestore 数据库中删除该记录。

我的代码仅在与查询匹配且任务成功时才有效,但不执行我的else部分代码。 这是为什么?

query匹配并且task is successful时,下面的代码可以正常工作,但是当没有匹配时,它不会执行else部分。 有谁知道为什么?

public void onClick(View view) {
        Task ref = fStore.collection("Favorites")
           .whereEqualTo("shopID", SID).whereEqualTo("usersID", UID)
                      .get()
          .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                       @Override
     public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
             for (QueryDocumentSnapshot document : task.getResult()) {
                              document.getReference().delete();
                                        }
                   } else {
      Map<String, Object> fav = new HashMap<>();
    
                         

         fav.put("shopID", SID);
         fav.put("usersID", UID);
         fav.put("ShopHeaderImg", sHI);
         fav.put("ShopProfileImg", sPI);
         fav.put("address", sA);
         fav.put("costEst", sCost);
         fav.put("country", sC);
         fav.put("latitude", sLat);
         fav.put("location", sL);
         fav.put("name", sN);
         fav.put("numTables", sNumTable);
         fav.put("ratings", sR);
         fav.put("summary", sSummary);
         fav.put("timing", sT);
    
    
                                      
      fStore.collection("Favorites").add(fav)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                       @Override
     public void onSuccess(DocumentReference documentReference) {
                                                Toast.makeText(DetailsActivity.this, "Saved", Toast.LENGTH_SHORT).show();
     }).addOnFailureListener(new OnFailureListener() {
    public void onFailure(@NonNull Exception e) {
   Toast.makeText(DetailsActivity.this, "Something went wrong", Toast.LENGTH_SHORT).show();
                                            }
                                        });
                                    }
                                }
                          

  });

我认为您误解了task.isSuccessful()的含义。 如果在查询执行期间没有错误,该方法将返回 true。 不返回任何文档的查询不是错误。 这种情况是完全正常的,并且被认为是成功的。 仅当您尝试执行 Firestore 实际无法运行的查询时才会发生错误。

如果要检查查询是否未返回任何文档,则应查看任务结果中包含的QuerySnapshot 它有一个方法isEmpty()会告诉你是否有文档。

public void onComplete(@NonNull Task<QuerySnapshot> task) {
    if (task.isSuccessful()) {
        QuerySnapshot querySnapshot = task.getResult();
        if (querySnapshot.isEmpty()) {
            // put code here to deal with no documents in the result
        }
        else {
            // put code here to deal with documents present in the result
            for (QueryDocumentSnapshot document : querySnapshot) {
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM