Android 从Firebase数据库中的嵌套节点检索数据
这是我的数据库结构: 请求节点的所有子节点都是时间戳。对于产品节点中的特定时间戳,我希望检索对象productName的所有值。然后对它们进行排列,使其成为电子邮件的主体。我陷入困境的部分是获取productName的数据 到目前为止,我已经能够在第二个节点中获取总数的数据。我使用的代码是:Android 从Firebase数据库中的嵌套节点检索数据,android,firebase-realtime-database,Android,Firebase Realtime Database,这是我的数据库结构: 请求节点的所有子节点都是时间戳。对于产品节点中的特定时间戳,我希望检索对象productName的所有值。然后对它们进行排列,使其成为电子邮件的主体。我陷入困境的部分是获取productName的数据 到目前为止,我已经能够在第二个节点中获取总数的数据。我使用的代码是: DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Requests"); ref.addLi
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Requests");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
collectProducts((Map<String, Object>) dataSnapshot.getValue());
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
private void collectProducts(Map<String, Object> value) {
ArrayList<String> products = new ArrayList<>();
for (Map.Entry<String, Object> entry : value.entrySet()){
Map singleUser = (Map) entry.getValue();
products.add((String) singleUser.get("total"));
}
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(Intent.EXTRA_EMAIL, "example@gmail.com");
StringBuilder sb = new StringBuilder();
for (String s : products) {
sb.append(s);
sb.append("\n");
}
emailIntent.putExtra(Intent.EXTRA_TEXT, sb.toString());
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
Log.i("Email sent!", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(Cart.this,
"There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
}
databasereferef=FirebaseDatabase.getInstance().getReference().child(“请求”);
ref.addListenerForSingleValueEvent(新的ValueEventListener(){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
collectProducts((Map)dataSnapshot.getValue());
}
@凌驾
已取消的公共void(DatabaseError DatabaseError){
}
});
私人产品(地图值){
ArrayList产品=新的ArrayList();
for(Map.Entry:value.entrySet()){
Map singleUser=(Map)entry.getValue();
products.add((字符串)singleUser.get(“total”);
}
意向emailIntent=新意向(Intent.ACTION\u SEND);
emailIntent.setType(“纯/文本”);
emailIntent.putExtra(Intent.EXTRA_EMAIL,“example@gmail.com");
StringBuilder sb=新的StringBuilder();
用于(字符串s:产品){
某人追加;
某人附加(“\n”);
}
emailIntent.putExtra(Intent.EXTRA_TEXT,sb.toString());
试一试{
startActivity(Intent.createChooser(emailIntent,“发送邮件…”);
Log.i(“已发送电子邮件!”,“”);
}捕获(android.content.ActivityNotFoundException ex){
Toast.makeText(Cart.this,
“没有安装电子邮件客户端。”,Toast.LENGTH_SHORT)。show();
}
}
但获得productName却不走运
我是Android和Firebase的新手。任何帮助都将不胜感激。像这样的事情应该可以做到:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Requests");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot requestSnapshot: dataSnapshot.getChildren()) {
DataSnapshot productsSnapshot = requestSnapshot.child("products");
for (DataSnapshot productSnapshot: productsSnapshot.getChildren()) {
System.out.println(productSnapshot.child("productName").getValue(String.class));
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
主要区别:
- 当用户对数据没有权限时,它会抛出一个显式错误
- 它使用
在子对象上循环DataSnapshot.getChildren()
- 它使用
来处理特定的子快照DataSnapshot.child()
- 它使用
检索原语/字符串值DataSnapshot.getValue()
- 像这样的东西应该可以做到:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Requests");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot requestSnapshot: dataSnapshot.getChildren()) {
DataSnapshot productsSnapshot = requestSnapshot.child("products");
for (DataSnapshot productSnapshot: productsSnapshot.getChildren()) {
System.out.println(productSnapshot.child("productName").getValue(String.class));
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
主要区别:
- 当用户对数据没有权限时,它会抛出一个显式错误
- 它使用
在子对象上循环DataSnapshot.getChildren()
- 它使用
来处理特定的子快照DataSnapshot.child()
- 它使用
检索原语/字符串值DataSnapshot.getValue()
ref.child(timestamp).addListenerForSingleValueEvent(new ValueEventListener(){
然后删除onDataChange()
中最外层的循环再次感谢Frank。感谢您的帮助。:)非常感谢。工作起来很有魅力:)哦,当然。:)但是如果不太麻烦的话,还有一件事。如果我想从特定的时间戳中获取所有产品的productName值,我该怎么做?为此,您需要ref.child(timestamp)。addListenerForSingleValueEvent(new ValueEventListener()){
然后移除onDataChange()中的最外层循环。
再次感谢Frank。感谢您的帮助。:)