如何遍历 Firebase 数据库 android (java) 中每个 ID 的某些 children?

how to iterate through some children's of every id in Firebase Database android (java)?

我有一个 parent 是商店,因为每个 id 都是一个商店,我想遍历每个 id 意味着每个商店并存储 shop_details 的 object 因为我想展示 RecyclerView

中的每家商店

这是我的数据库层次结构

这是我的部分代码,但这不起作用

ArrayList<Shop_details> list=new ArrayList<>();

database.getReference("Shops").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        list.clear();
        for (DataSnapshot postSnapshot: snapshot.getChildren()) {
            Shop_details shop_details = postSnapshot.getValue(Shop_details.class);
          
            list.add(shop_details);
          
        }
        shop_list_adapter adapter=new shop_list_adapter(list,getContext());
        shop_list_recyclerview.setAdapter(adapter);
        shop_list_recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        Loading_box.dismiss();

如果您的 Shop_details class 匹配数据库 shop_details 节点中的 JSON 结构,您应该只从该快照读取对象:

Shop_details shop_details = postSnapshot.child("shop_details").getValue(Shop_details.class);

请注意,最好将商店的不同方面存储在单独的 top-level 节点中,每个商店都使用相同的键。类似于:

shop_details: {
  "igd1P...": { owner_name: "...", shop_address: "...", ... },
  "yDLoU...": { owner_name: "...", shop_address: "...", ... }
},
shop_services: {
  "igd1P...": { ... },
  "yDLoU...": { ... }
}

这样您就可以只加载每家商店的详细信息,而无需加载他们的所有服务。这种类型的结构还可以让您更好地保护数据。

我建议阅读 structuring your data specifically on avoiding nested data and flattening your data structures