我有一些數據上檢索數據的正確元素,看起來像這樣: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()
如此,你可以在數據庫中看到,我應該得到所有child1
下uniqueId
小號除了uniqueId3
和uniqueId7
因爲這2個存在於child2
太多,而是我讓所有的uniqueId
小號除了uniqueId3
和uniqueId7
兩次uniqueId3
和uniqueId7
一次。
另一個要注意的是,當我在做這個條件爲lID.trim().equals(blackListedId.trim()
,我只得到匹配這一要求,其中2個IDS,即uniqueId3
和uniqueId7
如果child2
只有1個ID下uniqueId11
那麼我除了在這裏有一個,但有兩個或更多ID的所有uniqueId
正在導致這個問題。
我希望你得到了我的問題。我儘量用盡可能少的代碼來解釋它。
爲什麼!lID.trim().equals(blackListedId.trim()
被返回意外ID和我怎樣才能得到僅符合這個條件的ID?
@Ibrahim你不熟悉Firebase嗎? –
嗯,我認爲你的案例不依賴於firebase技能,它更多地依賴於調試技巧,嘗試調試值並檢查你的條件,我相信這是最快的解決方案。 – Ibrahim
記錄您從服務器獲取的所有內容。然後用代碼發佈你的logcat。 –