英文:
Can't get the Children in my firebase database
问题
[![Firebase数据库][1]][1]
  [1]: https://i.stack.imgur.com/o8k2v.jpg
我正在尝试获取节点“panadol”和“profinal”的这些值,但不起作用。
我已成功获取了日期。以下是我的代码示例:
    for (final String id: MedicinesListActivity.orderIdsList){
            // 获取订单日期
            DatabaseReference dateReference = FirebaseDatabase.getInstance()
                    .getReference("Orders").child(id);
            dateReference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    // 日期
                    String date = dataSnapshot.child("date").getValue(String.class);
                    Log.i("日期", date);
                    // 循环遍历特定订单ID中的所有产品
                    for (DataSnapshot s : dataSnapshot.child(MedicinesListActivity.userId).child("panadol").getChildren()){
                        Order order = s.getValue(Order.class);
                        Log.i("商品名称", "" + order.getName());
                    }
                }
            });
        }
MedicineListActivity.orderIds -> 包含我要循环遍历的所有订单ID
而类“Order”包含名称和订购数量。
但是它不起作用。
英文:
I'm trying to get these values in the node panadol and profinal. but it's not working.
I was able to get the date successfully. Here's my code
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 -> contains all the orderIds i want to loop through
and the class Order contains the name and the orderQuantity.
But it's not working.
答案1
得分: 1
为了解决这个问题,请使用以下代码行:
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()); //不要忽略错误!
    }
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
在日志中的输出将会是:
panadol/3.0
profinal/2.0
或者使用 Order 类:
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", order.getName() + "/" + order.getOrderQuantity());
        }
    }
    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d("TAG", databaseError.getMessage()); //不要忽略错误!
    }
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
结果将会是相同的。
在两种情况下,你必须使用所有节点名称在你的引用中,以便能够显示该数据。
英文:
To solve this, please use the following lines of code:
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);
The output in the logcat will be:
panadol/3.0
profinal/2.0
Or using the 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);
The result will be the same.
In both cases, you have to use all node names in your reference to be able to display that data.
答案2
得分: 1
在每个用户节点下,似乎有一个命名产品列表(panadol、profinal)。您的代码在该列表中使用 dataSnapshot.child(MedicinesListActivity.userId).child("panadol") 查找一个名为 panadol 的命名产品:
// 遍历特定订单 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 的各个属性:name 和 orderQuantity。但您的代码似乎尝试将这些属性中的每一个映射到整个 Order 对象,这样做是行不通的。
您有两个选择:
- 
显示各个属性,而不使用
Order类:for (DataSnapshot propSnapshot : dataSnapshot.child(MedicinesListActivity.userId).child("panadol").getChildren()) { Log.i("Item_Name", propSnapshot.getKey() + "=" + propSnapshot.getValue()); } - 
不使用循环,将产品及其属性获取到一个
Order对象中:for (DataSnapshot propSnapshot : dataSnapshot.child(MedicinesListActivity.userId).getChildren()) { Order order = s.getValue(Order.class); Log.i("Item_Name", propSnapshot.getKey() + "=" + propSnapshot.getValue()); } 
英文:
It seems like under each user's node, you have a list of named products (panadol, profinal). Your code looks up one named product panadol in that list with dataSnapshot.child(MedicinesListActivity.userId).child("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());
}
Since you then iterate over the child nodes of panadol, your s snapshot refers to the individual properties of panadol: name and orderQuantity. But your code seems to try to map each of those properties to an entire Order object, which won't work.
You have two options:
- 
Show the individual properties, without using the
Orderclass:for(DataSnapshot propSnapshot: dataSnapshot.child(MedicinesListActivity.userId).child("panadol").getChildren()){ Log.i("Item_Name", propSnapshot.getKey() + "="+ propSnapshot.getValue()); } - 
Don't use the loop, and get the products and their properties in an
Orderobject:for(DataSnapshot propSnapshot: dataSnapshot.child(MedicinesListActivity.userId).getChildren()){ Order order = s.getValue(Order.class); Log.i("Item_Name", propSnapshot.getKey() + "="+ propSnapshot.getValue()); } 
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论