从Firebase数据库中的嵌套节点中检索数据 [英] Retrieving data from nested nodes in Firebase Database

查看:57
本文介绍了从Firebase数据库中的嵌套节点中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数据库结构:

请求"节点的所有子代均为时间戳记.对于产品节点内的特定时间戳,我想检索对象productName的所有值.然后排列它们,使其成为电子邮件的主体.我遇到的问题是获取productName的数据.

All the children of the Request node are timestamps. For a particular timestamp, within the product node, i want to retrieve all the values of the object productName. And then arrange them so that it is composed as the body of an email. The part where i am stuck at is in fetching the data of productName.

到目前为止,我已经能够在第二个节点中获取总数的数据.我正在使用的代码是这样的:

So far i've been able to fetch the data of the total in the second node. the code i'm using is this:

    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();
    }

}

但是获得productName并没有运气.

But no luck with getting productName.

我是Android和Firebase的新手.感谢您的帮助.

I am new to Android and Firebase. Any help is appreciated.

推荐答案

这种方法应该可以解决问题:

Something like this should do the trick:

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()检索基本值/字符串值.
  • It throws an explicit error when the user doesn't have permission on the data.
  • It uses DataSnapshot.getChildren() to loop over children.
  • It uses DataSnapshot.child() to address a specific child snapshot.
  • It uses DataSnapshot.getValue() to retrieve the primitive/String value.

这篇关于从Firebase数据库中的嵌套节点中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆