2017-10-16 55 views
5

我有一些数据上检索数据的正确元素,看起来像这样:if语句不<code>FirebaseDatabase</code>返回从火力

app 
-child1 
    -uniqueId1 
    -pId1 
    -lId1 
    -uniqueId2 
    -pId2 
    -lId2 
    -uniqueId3 
    -pId3 
    -lId3 
    -uniqueId4 
    -pId4 
    -lId4 
    -uniqueId5 
    -pId5 
    -lId5 
    -uniqueId6 
    -pId6 
    -lId6 
-child2 
    -uniqueIdA1 
    -uniqueId7 
    -uniqueId8 
    -uniqueId9 
    -uniqueId10 
    -uniqueId11 
    -uniqueId1 
    -uniqueId2 
    -uniqueId3 
    -uniqueId4 
    -uniqueId5 

我检索child1的数据是这样的:

public void fMethod(final String fID, final String blackListedId) { 
     mDatabase.child("child1").addChildEventListener(new ChildEventListener() { 
      @Override 
      public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
       if (dataSnapshot.getValue() != null) { 
        Profile profile = dataSnapshot.getValue(Profile.class); 
        String pID = profile.getPID(); 
        String lID = profile.getLID(); 
        if (!pID.trim().equals(AccessToken.getCurrentAccessToken().getUserId().trim())) { 
         if (pID.trim().equals(fID.trim())) { 
          if (!lID.trim().equals(blackListedId.trim())) { 
          // populate the view with elements which meet this condition/requirement 

          String listingID = profile.getlID(); 
          Log.d("LISTING_IDS", listingID); 

          } else { 
           Log.d("dataSnapshot", "null1"); 
          } 
         } else { 
          Log.d("dataSnapshot", "null2"); 
         } 
        } else { 
         Log.d("dataSnapshot", "null3"); 
        } 
       } else { 
        Log.d("dataSnapshot", "null4"); 
       } 
      } 
      ... 
      ... 
      ... 
    } 

child2的数据是这样的:

public void fData(final String fID) { 

     mDatabase.child("child2").child(AccessToken.getCurrentAccessToken().getUserId()).addListenerForSingleValueEvent(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       if (dataSnapshot.getValue() != null) { 
        for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) { 
         String blackListedId = childSnapshot.getValue().toString(); 
         fMethod(fID, blackListedId); 
        } 
       } else { 
        fMethod(fID, ""); 
       } 
      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 
    } 

然后在另一个代码中,我正在检索fID s,并在那里调用fData()方法。

我记录所有的ID我从数据库中获取:

D/LISTING_IDS: uniqueId1 
D/LISTING_IDS: uniqueId1 
D/LISTING_IDS: uniqueId1 
D/LISTING_IDS: uniqueId1 
D/LISTING_IDS: uniqueId1 
D/LISTING_IDS: uniqueId1 
D/LISTING_IDS: uniqueId1 
D/LISTING_IDS: uniqueId1 
D/LISTING_IDS: uniqueId1 
D/LISTING_IDS: uniqueId2 
D/LISTING_IDS: uniqueId2 
D/LISTING_IDS: uniqueId2 
D/LISTING_IDS: uniqueId2 
D/LISTING_IDS: uniqueId2 
D/LISTING_IDS: uniqueId2 
D/LISTING_IDS: uniqueId2 
D/LISTING_IDS: uniqueId2 
D/LISTING_IDS: uniqueId2 
D/LISTING_IDS: uniqueId3 
D/LISTING_IDS: uniqueId3 
D/LISTING_IDS: uniqueId3 
D/LISTING_IDS: uniqueId3 
D/LISTING_IDS: uniqueId3 
D/LISTING_IDS: uniqueId3 
D/LISTING_IDS: uniqueId3 
D/LISTING_IDS: uniqueId3 
D/LISTING_IDS: uniqueId3 
D/LISTING_IDS: uniqueId4 
D/LISTING_IDS: uniqueId4 
D/LISTING_IDS: uniqueId4 
D/LISTING_IDS: uniqueId4 
D/LISTING_IDS: uniqueId4 
D/LISTING_IDS: uniqueId4 
D/LISTING_IDS: uniqueId4 
D/LISTING_IDS: uniqueId4 
D/LISTING_IDS: uniqueId4 
D/LISTING_IDS: uniqueId5 
D/LISTING_IDS: uniqueId5 
D/LISTING_IDS: uniqueId5 
D/LISTING_IDS: uniqueId5 
D/LISTING_IDS: uniqueId5 
D/LISTING_IDS: uniqueId5 
D/LISTING_IDS: uniqueId5 
D/LISTING_IDS: uniqueId5 
D/LISTING_IDS: uniqueId5 

这里的Profile.java文件的代码:https://gist.github.com/HammadNasir/a196bcdc6dccbf69657fca528443e680

的问题是,在fMethod()的if语句的条件是!lID.trim().equals(blackListedId.trim()如此,你可以在数据库中看到,我应该得到所有child1uniqueId小号除了uniqueId3uniqueId7因为这2个存在于child2太多,而是我让所有的uniqueId小号除了uniqueId3uniqueId7两次uniqueId3uniqueId7一次。

另一个要注意的是,当我在做这个条件为lID.trim().equals(blackListedId.trim(),我只得到匹配这一要求,其中2个IDS,即uniqueId3uniqueId7如果child2只有1个ID下uniqueId11那么我除了在这里有一个,但有两个或更多ID的所有uniqueId正在导致这个问题。

我希望你得到了我的问题。我尽量用尽可能少的代码来解释它。

为什么!lID.trim().equals(blackListedId.trim()被返回意外ID和我怎样才能得到仅符合这个条件的ID?

+0

@Ibrahim你不熟悉Firebase吗? –

+1

嗯,我认为你的案例不依赖于firebase技能,它更多地依赖于调试技巧,尝试调试值并检查你的条件,我相信这是最快的解决方案。 – Ibrahim

+1

记录您从服务器获取的所有内容。然后用代码发布你的logcat。 –

回答

0

儿童2 addListenerForSingleValueEvent总是会调用两次 - 一次是当你将它设置和第二时已读取所有数据。所以当它第一次被调用时,它最终会调用fMethod(fID, ""),这是你从Child1获得所有ID的地方,包括3和7.但是下次它调用它时,它描述的就会很好。所以如果你从你的child2 ValueEventListener中删除"else"条件,我认为它应该可以正常工作。

让我知道如果我明白你的问题,回答了这个问题。如果没有,请随时解释更多细节。

+0

不,Abhishek。这没有发生。我在'else'代码中添加了一个'log'语句,但它没有被调用。 –