Firebase getValue()没有正确检索布尔值?

时间:2016-07-30 07:56:35

标签: android firebase boolean firebase-realtime-database

如果值只是true或false,我在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

在我想在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可以是我们可以检索的一种数据,但我似乎无法正确检索它。任何帮助,将不胜感激。谢谢!

3 个答案:

答案 0 :(得分:4)

适合我:

    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) {

                       }
                   });
               }
     });
  

https://stackoverflow.firebaseio.com/38671701/users/10206287838487450/following

上倾听孩子的声音      

10208425049208936 =真

     

107214969710378 =假

     

1789986447904975 =真

答案 1 :(得分:0)

尝试这样的事情:

   Firebase followingRef = currentUserPath.child("following");
   followingRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
              for(DataSnapshot snapshot: dataSnapshot.getChildren()){
                  Log.d(TAG, snapshot.getKey();
                  Log.d(TAG, snapshot.getValue();

                 //Do your operations here
              }
        }
    }

答案 2 :(得分:-1)

更改模型类

布尔值值 至 布尔