Firebase遍历整个数据库

问题描述 投票:0回答:1

我试图从firebase数据库接收所有数据,所以我可以将它放入Users类但是我不知道如何遍历整个数据库,因为我无法插入(for循环中的i变量)插入值进入儿童领域。

我的代码是这样的:

myRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        if(dataSnapshot.exists())
        {
            String fullname = (String) dataSnapshot.child(userId).child("Fullname").getValue();
            String country = (String) dataSnapshot.child(userId).child("Country").getValue();
            String profilePicture = (String) dataSnapshot.child(userId).child("ProfilePicture").getValue();
            String username = (String) dataSnapshot.child(userId).child("Username").getValue();
            Toast.makeText(getApplicationContext(), "Testing with: " + fullname + country + profilePicture + username, Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(), "Count : (4) - " + dataSnapshot.getChildrenCount(), Toast.LENGTH_SHORT).show();
            showProfiles(dataSnapshot);
        }
    }

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

    }
});

数据库:

{
      "Users" : {
        "4PdlTlv3qjZ3BmDvrJyUut9Fnq43" : {
          "Country" : "xx",
          "Fullname" : "hh",
          "ProfilePicture" : "htm/o/Images%2FSearchAdapter%2F4PdlTlv3qjZ3BmDvrJyUut9Fnq43%2FProfilePicture%2FProfilePic_2019.03.06.10.47.54.jpg.jpg?alt=media&token=b647708e-c6d5-4b45-bef0-3dc40301b73a",
          "Username" : "hmack001"
        },
        "COg4r4io9hezhFpmK3adPucUXA93" : {
          "Country" : "spain",
          "Fullname" : "nat",
          "ProfilePicture" : "hcom/o/Images%2FSearchAdapter%2FCOg4r4io9hezhFpmK3adPucUXA93%2FProfilePicture%2FProfilePic_2019.03.06.19.14.17.jpg.jpg?alt=media&token=8620b321-5cef-42f0-a828-dbb7c37c8e7d",
          "Username" : "nat"
        },
        "Tw1xRxViygNsLqrQiaaMAvAduIu1" : {
          "Country" : "uk",
          "Fullname" : "harvey\n",
          "ProfilePicture" : "t.com/o/Images%2FUsers%2FTw1xRxViygNsLqrQiaaMAvAduIu1%2FProfilePicture%2FProfilePic_2019.03.03.05.26.35.jpg.jpg?alt=media&token=c290e75a-5f92-4271-bcb5-c644fe1b14ef",
          "Username" : "RGB"
        },
        "vOxr1RoDqgWogKK1lp9pfpTHc6w2" : {
          "Country" : "scotland ",
          "Fullname" : "greg greg",
          "ProfilePicture" : "ot.com/o/Images%2FSearchAdapter%2FvOxr1RoDqgWogKK1lp9pfpTHc6w2%2FProfilePicture%2FProfilePic_2019.03.04.12.30.22.jpg.jpg?alt=media&token=27b024cf-0691-4121-8a27-26acf101ebc2",
          "Username" : "greg"
        },
        "xecUOPeyMcQaQrgkU9ouDgK90Ai1" : {
          "Country" : "ggh",
          "Fullname" : "Da apply ",
          "ProfilePicture" : "2FProfilePic_2019.03.03.04.58.50.jpg.jpg?alt=media&token=f35854c2-3ff9-4d18-9f7a-10c13f066c68",
          "Username" : "gg"
        }
      }
    }

我知道在这段代码中没有尝试从所有用户字段中检索信息,我不明白迭代数据库背后的逻辑,因为我没有看到我可以做的事情,项目1,项目2等。

android firebase firebase-realtime-database
1个回答
1
投票

用这个

for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
  System.out.println(childSnapshot.getKey());
  System.out.println(childSnapshot.child("Country").getValue(String.class));
}
© www.soinside.com 2019 - 2024. All rights reserved.