Firebase遍历整个数据库

时间:2019-03-08 18:25:08

标签: android firebase firebase-realtime-database

我正在尝试从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等。< / p>

1 个答案:

答案 0 :(得分:1)

使用此

for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
  System.out.println(childSnapshot.getKey());
  System.out.println(childSnapshot.child("Country").getValue(String.class));
}