Android从Firebase数据快照中提取数据

时间:2017-03-22 11:35:58

标签: android firebase firebase-realtime-database

我正在尝试从firebase获取DataSnapshot中的数据。我按照firebase开发人员示例中的说明进行操作。当我尝试拉取数据时,它会显示为null,尽管我在Android Studio中运行调试器时可以看到数据。下面列出的是我正在使用的代码。

    Query mQueryRef = mDatabase.child("users").child(mUserId).child("bullets");
    // This type of listener is not one time, and you need to cancel it to stop
    // receiving updates.
    mQueryRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot snapshot, String previousChild) {
            // This will fire for each matching child node.
            Bullet bullet = snapshot.getValue(Bullet.class);
            String date = bullet.getDate();
            String title = bullet.getTitle();
            mEmailBody + "\n" + title + " " + date;

这是Bullet类:

public class Bullet {

// Bullet basics
private String title;
private String orderDate;
private String date;
private String action;
private String result;
private String impact;

// Default constructor required by Firebase
public Bullet (){

}

//Constructor
public Bullet (String title, String orderDate, String date, String action, String result, String impact) {
    this.title = title;
    this.orderDate = orderDate;
    this.date = date;
    this.action = action;
    this.result = result;
    this.impact = impact;

}

//Getters
public String getTitle() {return title;}
public String getOrderDate() {return orderDate;}
public String getDate() {return date;}
public String getAction() {return action;}
public String getResult() {return result;}
public String getImpact() {return impact;}

//Setters
public void setTitle(String title){
    this.title = title;
}
public void setOrderDate(String orderDate){
    this.orderDate = orderDate;
}
public void setDate(String date){
    this.date = date;
}
public void setAction(String action){
    this.action = action;
}
public void setResult(String result){
    this.result = result;
}
public void setImpact(String impact){
    this.impact = impact;
}

}

这是来自firebase的json数据表:

- appname-1234b
        - users
            - WRQK8Fo3TPUnZyPXYnQ9gHVfFms2
                - bullets
                    - Kfc8jCy5f2bb1aN-o0C
                        - bullet
                          action: "did some stuff"
                          date: "19 Mar 2017"
                          impact: "some stuff was impacted"
                          orderDate: "2017078"
                          result: "a result happened"
                          title: "new one for the date"
                    - KfcMg-7-xp98Kwq-PUR
                        - bullet 
                          action: "did some stuff"
                          date: "19 Mar 2017"
                          impact: "some stuff was impacted"
                          orderDate: "2017078"
                          result: "a result happened"
                          title: "new one for the date"

当我运行调试器时,它到达行Bullet bullet = snapshot.getValue(Bullet.class); 我可以看到所有数据点看起来像Bullet: snapshot {key: Kfc8jCy5f2bb1aN-o0C title:"new one for the date" date:"19 Mar 2017"......}但是当它到达下两行时

String date = bullet.getDate();
String title = bullet.getTitle();

调试器将值显示为date = null bullet:3248btitle = null bullet:3248b

我尝试了很多不同的数据提取方式:

String date = snapshot.getValue(Bullet.Class).getDate();
String textBullet = snapshot.getValue(Bullet.Class).toString();

但每次出现null或者整个子弹数据都是不可提取的。

3 个答案:

答案 0 :(得分:3)

您在每个推送ID下都有一个名为expandSearchFilter :function ( me , eOpts) { Ext.getBody().mask(); }, colapseSearchFilter:function ( me , eOpts) { Ext.getBody().unmask(); } 的孩子,但您不会在听众中考虑这一点。

最简单的解决方案是跳过代码中的bullet节点:

bullet

但更好的解决方案是将您的数据结构更改为不具有Query mQueryRef = mDatabase.child("users").child(mUserId).child("bullets"); // This type of listener is not one time, and you need to cancel it to stop // receiving updates. mQueryRef.addChildEventListener(new ChildEventListener() { @Override public void onChildAdded(DataSnapshot snapshot, String previousChild) { Bullet bullet = snapshot.child("bullet").getValue(Bullet.class); // ^^^^^^^^^^^^^^^^ String date = bullet.getDate(); String title = bullet.getTitle(); 级别。如果您未在代码中使用它,则无法在数据库中使用它。

答案 1 :(得分:1)

您的数据库引用指向项目符号,其值为HashMap而不是直接的Bullet对象。您需要Hashmap来检索数据。

private static HashMap<String, Bullet> bullets = new HashMap<>(); 

Query mQueryRef = mDatabase.child("users").child(mUserId).child("bullets");
    // This type of listener is not one time, and you need to cancel it to stop
    // receiving updates.
    mQueryRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                Log.d(TAG, "onDataChange():" + dataSnapshot.toString());

                for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
                    Bullet bullet = snapshot.getValue(Bullet.class);
                    bullets.put(snapshot.getKey(), bullet);
                }

               //Iterate and Print your bullets hashmap here.
        System.out.println(Arrays.asList(bullets)); 
            }

            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
                Log.e(TAG, "Failed to read Bullet list.", error.toException());

            }
        });
    }

答案 2 :(得分:0)

根据DataSnapshot文档:

&#34; DataSnapshots传递给您使用addValueEventListener(ValueEventListener)附加的侦听器中的方法, addChildEventListener(ChildEventListener),或 addListenerForSingleValueEvent(ValueEventListener)&#34;