【问题标题】:How do I retrieve all child data from a specific node in Firebase on Android?如何从 Android 上的 Firebase 中的特定节点检索所有子数据?
【发布时间】:2019-07-13 11:15:41
【问题描述】:

我有一个通过异步加载的 firebase 数据库,我可以验证所有数据是否存在于正确的节点中,但由于某种原因,我无法弄清楚如何调用存储在特定节点中的数据。

我已经尝试过 .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
    },

我创建了我的 Firebase 数据库,其中 movieID 是键值,而不是生成的值,因为我的 id 将始终是唯一的。

现在,当我尝试通过以下方法将快照存储为我的对象时,当我尝试推断一些对象数据以更新 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) {

        }
    });

对于我为什么在这里遇到这么多困难有什么想法或想法吗?

【问题讨论】:

  • 您需要遍历数据库。类似 for (DataSnapshot data : dataSnapshot.getChildren()) { if (data.getKey().equals(movieId) { ..//do stuff here } }
  • 我也试过这个,但我认为这是因为数据是静态的(未添加/更改)我的 ValueListener 实际上没有被调用。我在我的加载活动中异步加载 Firebase,它正在验证不存在重复项,所以当我进入我的详细信息活动(这段代码在哪里)时,我的数据不是 new 如果那样的话有道理...知道如何在不依赖 onDataChanged 的​​情况下查询 Firebase?

标签: android firebase firebase-realtime-database


【解决方案1】:

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

在您的 JSON 中,它显示 movieID 是一个数值:

"movieID" : 297802

在你做的代码中:

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

这意味着你在比较整数和字符串,它总是返回 false。解决办法是用数字比较数字:

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

【讨论】:

  • 我希望看到你回答我的问题,弗兰克! - 但我似乎仍然无法将任何数据加载到我的 UI 中。如果我在 Firebase 中有静态数据(即:它是 2 周前加载的),我如何调用该数据并将其加载到 UI 中 - 因为它没有添加或更改 - 它就在那里......
  • onChildAdded 会立即为每个现有的孩子调用。如果您对此有疑问,请使用minimal, complete code that reproduces the problem 发布新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-29
  • 2019-02-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多