use*_*869 2 android boolean firebase firebase-realtime-database
我在使用 firebase 调用时遇到问题,我想比较该值是真还是假。这是我的尝试:
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
Run Code Online (Sandbox Code Playgroud)
在我想要引用的路径中,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"
}
},
Run Code Online (Sandbox Code Playgroud)
我很困惑,因为每次我尝试将dataSnapshot.getValue()转换为布尔值时,我总是得到isFollowing = true这显然是不正确的,因为在我的数据中,只有 2 个值是正确的,如上所示,而不是全部 3 个。令人困惑的是为什么我无法Boolean正确检索。我也尝试过,Boolean isFollowing = (Boolean) dataSnapshot.getValue(Boolean.class)但这不起作用。Firebase 文档说,在检索数据时, aBoolean可以是我们可以检索的一种数据类型,但我似乎无法正确检索它。任何帮助,将不胜感激。谢谢!
对我有用:
Firebase ref = new Firebase("https://stackoverflow.firebaseio.com/38671701/users");
Firebase currentUserPath = ref.child("10206287838487450");
Firebase firebaseRef = ref.child("10206287838487450");
Firebase followingRef = currentUserPath.child("/following");
System.out.println("Listening for children on "+followingRef);
followingRef.addChildEventListener(new ChildEventListenerBase() {
@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();
System.out.println(dataSnapshot.getKey()+"="+isFollowing);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
在https://stackoverflow.firebaseio.com/38671701/users/10206287838487450/following上聆听儿童
10208425049208936=真
107214969710378=假
1789986447904975=真
| 归档时间: |
|
| 查看次数: |
8797 次 |
| 最近记录: |