[英]How to get the child from firebase in android studio?
In Firebase Realtime Database I want to show the user the list of orders that has been placed and for that, I need to get all the child that is created in Order Along with the it's value.在 Firebase 实时数据库中,我想向用户显示已下订单的列表,为此,我需要获取在订单中创建的所有子项及其值。
This is the code I have done so far这是我到目前为止所做的代码
FirebaseDatabase.getInstance().getReference().child("Users").child("Customer").child(userID).child("Order").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
Users usersnapshot = snapshot.getValue(Users.class);
if(usersnapshot!=null){
mCustomerinfo.setVisibility(View.VISIBLE);
Map<String, Object> map = (Map<String, Object>) snapshot.getValue();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
public void Item( Map<String,Object> map){
// TODO:To check all the item in order and show it to the user
}
If you only need the value of the "Egg" property, create a reference that points to the exact property and get it's value as in following lines of code:如果您只需要“Egg”属性的值,请创建一个指向确切属性的引用并获取它的值,如以下代码行所示:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference eggRef = rootRef.child("Users").child("Customer").child(uid).child("Order").child("Egg");
productsRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> task) {
if (task.isSuccessful()) {
String value = task.getResult().getValue(String.class);
Log.d("TAG", value);
} else {
Log.d("TAG", task.getException().getMessage()); //Don't ignore potential errors!
}
}
});
The result in the logcat will be: logcat 中的结果将是:
10
If you want a user to have multiple orders, you should consider using a schema that looks like this:如果您希望用户有多个订单,您应该考虑使用如下所示的架构:
Firebase-root
|
--- Users
|
--- $uid
|
--- orders
|
--- $pushedId
| |
| --- //Order details
|
--- $pushedId
|
--- //Order details
So you need to differentiate each order by calling push() method.所以你需要通过调用 push() 方法来区分每个订单。
Edit:编辑:
According to your last comment:根据您最后的评论:
I wanted to know the code for getting multiple children inside the order (Suppose Egg:10 Onion:5 and so on ) so how do I get that?我想知道在订单中获取多个子项的代码(假设 Egg:10 Onion:5 等等),那么我该如何获取呢?
Here is your code:这是您的代码:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference orderRef = rootRef.child("Users").child("Customer").child(uid).child("Order");
orderRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> task) {
if (task.isSuccessful()) {
for (DataSnapshot ds : task.getResult().getChildren()) {
String key = ds.getKey();
String value = ds.getValue(String.class);
Log.d("TAG", key + ":" + value);
}
} else {
Log.d("TAG", task.getException().getMessage()); //Don't ignore potential errors!
}
}
});
The result in the logcat will be: logcat 中的结果将是:
Egg:10
Onion:5
The result in the结果在
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.