繁体   English   中英

无法在我的 firebase 数据库中获取孩子

[英]Can't get the Children in my firebase database

火力数据库

我试图在节点 panadol 和 profinal 中获取这些值。 但它不起作用。 我能够成功获得日期。 这是我的代码

for(final String id: MedicinesListActivity.orderIdsList){

        //get the date of the order
        DatabaseReference dateReference = FirebaseDatabase.getInstance()
                .getReference("Orders").child(id);
        dateReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                //date
                String date = dataSnapshot.child("date").getValue(String.class);
                Log.i("Date", date);

                //loop through all the products in the specific order id
                for(DataSnapshot s : dataSnapshot.child(MedicinesListActivity.userId).child("panadol").getChildren()){
                    Order order = s.getValue(Order.class);
                    Log.i("Item_Name", ""+ order.getName());
                }

            }
        });
    }

MedicineListActivity.orderIds -> 包含我想要循环的所有 orderIds,并且 class Order包含名称和 orderQuantity。 但它不起作用。

要解决此问题,请使用以下代码行:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Orders").child(id).child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String name = ds.child("name").getValue(String.class);
            String orderQuantity = ds.child("orderQuantity").getValue(String.class);
            Log.d("TAG", name + "/" + orderQuantity);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
    }
};
uidRef.addListenerForSingleValueEvent(valueEventListener);

logcat 中的 output 将是:

panadol/3.0
profinal/2.0

或使用Order class:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Orders").child(id).child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            Order order = ds.getValue(Order.class);
            Log.d("TAG", ds.getName() + "/" + ds.getOrderQuantity);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
    }
};
uidRef.addListenerForSingleValueEvent(valueEventListener);

结果将是相同的。

在这两种情况下,您都必须使用参考中的所有节点名称才能显示该数据。

似乎在每个用户的节点下,您都有一个命名产品列表(panadol,profinal)。 您的代码使用dataSnapshot.child(MedicinesListActivity.userId).child("panadol")在该列表中查找一个名为panadol的产品:

//loop through all the products in the specific order id
for(DataSnapshot s : dataSnapshot.child(MedicinesListActivity.userId).child("panadol").getChildren()){
    Order order = s.getValue(Order.class);
    Log.i("Item_Name", ""+ order.getName());
}

由于您随后遍历panadol的子节点,因此您s快照指的是panadol的各个属性: nameorderQuantity 但是您的代码似乎尝试将这些属性中的每一个 map 转换为整个Order object,但这是行不通的。

你有两个选择:

  • 显示单个属性,不使用Order class:

     for(DataSnapshot propSnapshot: dataSnapshot.child(MedicinesListActivity.userId).child("panadol").getChildren()){ Log.i("Item_Name", propSnapshot.getKey() + "="+ propSnapshot.getValue()); }
  • 不要使用循环,并在Order object 中获取产品及其属性:

     for(DataSnapshot propSnapshot: dataSnapshot.child(MedicinesListActivity.userId).getChildren()){ Order order = s.getValue(Order.class); Log.i("Item_Name", propSnapshot.getKey() + "="+ propSnapshot.getValue()); }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM