如果值只是true或false,那麼我想要比較的firebase調用遇到問題。這裏是我的嘗試:Firebase getValue()不正確檢索布爾值?
Firebase followingRef = currentUserPath.child("/following");
followingRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(final DataSnapshot dataSnapshot, String s) {
firebaseRef.child("people/" + dataSnapshot.getKey()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
Boolean isFollowing = (Boolean) dataSnapshot.getValue();
if(isFolloiwng) {
User user = snapshot.getValue(User.class);
user.setUserId(dataSnapshot.getKey());
application.getFollowingRecyclerViewAdapter().add(user, application.getFollowingRecyclerViewAdapter());
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
...//OnChildChanged,Moved,etc... no code in here
在我想在currentUserPath.child("following");
引用路徑它看起來像這樣:
編輯:實際JSON替換圖片:
"users" : {
"10206287838487450" : {
"following" : {
"10208425049208936" : true,
"107214969710378" : false,
"1789986447904975" : true
},
"id" : "ae80f030-dea0-4909-944f-0e490847b6ab",
"name" : "Stephen",
"posts" : {
"-KN_oJAgTNJHwJbqY3qk" : true,
"-KN_oN9_Xmw5ULnBRYM7" : true,
"-KN_obYGug9Tdrzufbqc" : true,
"-KNefMk2nX0sOsUx0btx" : true
},
"userId" : "10206287838487450"
},
"people" : {
"10206287838487450" : {
"id" : "9e7ee838-a60a-4588-bc1c-93b98f74356d",
"name" : "Bob",
"userId" : "10206287838487450"
},
"10208425049208936" : {
"id" : "fe6f97e3-0efb-4afb-a322-a1c586f75fb7",
"name" : "Jack",
"userId" : "10208425049208936"
},
"107214969710378" : {
"id" : "ae80f030-dea0-4909-944f-0e490847b6ab",
"name" : "Rick",
"userId" : "107214969710378"
},
"108421236267392" : {
"id" : "c72b35d9-380b-4552-8b05-7426d378fa14",
"name" : "Tommy",
"userId" : "108421236267392"
},
"1112460595485164" : {
"id" : "383692f0-0aba-4d29-afb8-80beefe678c6",
"name" : "Stanley",
"userId" : "1112460595485164"
},
"1789986447904975" : {
"id" : "1ae43255-c040-4b1e-959e-fcdf03e13a45",
"name" : "Link",
"userId" : "1789986447904975"
}
},
我很困惑因爲每次我試圖將dataSnapshot.getValue()
轉換爲布爾值時,我總是得到isFollowing = true
,這顯然是不正確的,因爲在我的數據中,只有2個值是真實的,如上所示,而不是全部3個。令人困惑的是我怎麼會無法檢索Boolean
正確。我也嘗試過Boolean isFollowing = (Boolean) dataSnapshot.getValue(Boolean.class)
,但這不起作用。 Firebase文檔說,檢索數據時,Boolean
可以是我們可以檢索的一種數據,但似乎無法正確檢索。任何幫助,將不勝感激。謝謝!
你已經包括了JSON樹在你的問題的照片。請用實際的JSON替換爲文本,您可以通過點擊Firebase數據庫控制檯中的導出按鈕輕鬆獲取。將JSON作爲文本可以搜索,使我們能夠輕鬆使用它來測試您的實際數據,並將其用於我們的答案中,並且通常只是一件好事。 –
@FrankvanPuffelen對於缺乏清晰度感到抱歉。我已經包含了該數據庫調用的JSON。感謝您幫助我使我的帖子更清晰!任何想法爲什麼這可能會發生在我身上? – user1871869
在剛剛發佈的JSON中,'people'節點*位於'users'節點下面(所以'/ users/people')。這確實是你使用的結構? –