简体   繁体   English

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

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

i created a feature that lets my app users favorite shops.我创建了一个功能,让我的应用程序用户最喜欢的商店。 now if a user already favorited a shop and clicks on the button again it should delete the record from the firestore database.现在,如果用户已经收藏了一家商店并再次单击该按钮,它应该从 Firestore 数据库中删除该记录。

my code works only if there is a match to the query and the task is successful, but doesnt execute my else part of the code.我的代码仅在与查询匹配且任务成功时才有效,但不执行我的else部分代码。 why is that?这是为什么?

and here isthe below code works fine when there is a match for the query and the task is successful , but when there isn't it doesnt execute the else part.query匹配并且task is successful时,下面的代码可以正常工作,但是当没有匹配时,它不会执行else部分。 anyone know why?有谁知道为什么?

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();
                                            }
                                        });
                                    }
                                }
                          

  });

I think you are misunderstanding what task.isSuccessful() means.我认为您误解了task.isSuccessful()的含义。 That method will return true if there were no errors during the execution of the query.如果在查询执行期间没有错误,该方法将返回 true。 A query that returns no documents isn't an error.不返回任何文档的查询不是错误。 That situation is perfectly normal, and considered successful.这种情况是完全正常的,并且被认为是成功的。 An error only happens when you try to execute a query that Firestore can't actually run.仅当您尝试执行 Firestore 实际无法运行的查询时才会发生错误。

If you want to check if a query returned no documents, you should look at the QuerySnapshot contained in the task results.如果要检查查询是否未返回任何文档,则应查看任务结果中包含的QuerySnapshot It has a method isEmpty() that will tell you if there are documents.它有一个方法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