我有一些数据上检索数据的正确元素,看起来像这样: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。 –