如何在Android的Firebase中从特定节点检索所有子数据?

时间:2019-02-19 21:07:09

标签: android firebase firebase-realtime-database

我有一个通过Async加载的firebase数据库,可以验证所有数据都存在于正确的节点中,但是由于某种原因,我不知道如何调用存储在特定节点中的数据。 / p>

我已经尝试过.addChildEventListener和.addValueEventListener,但是似乎没有什么允许我的全局对象在特定节点上加载详细信息。

下面是每个节点的json快速快照。

 {
 "movies" : {
    "297802" : {
      "movieBackdrop" : "/5A2bMlLfJrAfX9bqAibOL2gCruF.jpg",
      "movieFavorite" : 0,
      "movieID" : 297802,
      "movieLanguage" : "en",
      "movieOverview" : "Once home to the most advanced civilization on Earth, the city of Atlantis is now an underwater kingdom ruled by the power-hungry King Orm. With a vast army at his disposal, Orm plans to conquer the remaining oceanic people -- and then the surface world. Standing in his way is Aquaman, Orm's half-human, half-Atlantean brother and true heir to the throne. With help from royal counselor Vulko, Aquaman must retrieve the legendary Trident of Atlan and embrace his destiny as protector of the deep.",
      "moviePopularity" : 116,
      "moviePosterPath" : "/5Kg76ldv7VxeX9YlcQXiowHgdX6.jpg",
      "movieReleaseDate" : "2018-12-07",
      "movieTitle" : "Aquaman",
      "movieVoteAverage" : 6.8,
      "movieVoteCount" : 3668
    },
    "299536" : {
      "movieBackdrop" : "/bOGkgRGdhrBYJSLpXaxhXVstddV.jpg",
      "movieFavorite" : 0,
      "movieID" : 299536,
      "movieLanguage" : "en",
      "movieOverview" : "As the Avengers and their allies have continued to protect the world from threats too large for any one hero to handle, a new danger has emerged from the cosmic shadows: Thanos. A despot of intergalactic infamy, his goal is to collect all six Infinity Stones, artifacts of unimaginable power, and use them to inflict his twisted will on all of reality. Everything the Avengers have fought for has led up to this moment - the fate of Earth and existence itself has never been more uncertain.",
      "moviePopularity" : 109,
      "moviePosterPath" : "/7WsyChQLEftFiDOVTGkv3hFpyyt.jpg",
      "movieReleaseDate" : "2018-04-25",
      "movieTitle" : "Avengers: Infinity War",
      "movieVoteAverage" : 8.3,
      "movieVoteCount" : 11448
    },

由于我的id始终是唯一的,因此我以movieID作为键值而不是生成的值创建了Firebase数据库。

现在,当我尝试通过以下方法将快照存储为对象时,当我尝试外推某些对象数据以更新UI时,始终出现指向我的对象currentMovie的空错误。我知道当我检查.equalTo(String.valueOf(movieID)时-它传递的值与应该正确匹配的有效值相关,但是我不断收到null错误。

mFirebaseDatabase = FirebaseDatabase.getInstance();
    mMovieDatabaseReference = mFirebaseDatabase.getReference().child("movies");
    Query movieQuery = mMovieDatabaseReference.orderByChild("movieID").equalTo(String.valueOf(movieID));
    movieQuery.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            currentMovie = dataSnapshot.getValue(Movie.class);
        }

        @Override
        public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

关于我为什么在这里遇到这么多困难的任何想法或想法?

1 个答案:

答案 0 :(得分:1)

您正在比较字符串和整数。

在您的JSON中,它显示[[13.0, 23.0, 33.0], [3.0, 6.0, 9.0], [3.0]] 是一个数字值:

movieID

在代码中,您将执行以下操作:

"movieID" : 297802

因此,这意味着您正在比较整数和字符串,它们始终返回false。解决方案是将数字与数字进行比较:

mMovieDatabaseReference.orderByChild("movieID").equalTo(String.valueOf(movieID))