我正在尝试从firebase的DataSnapshot中获取数据。我遵循了firebase开发人员示例中的说明。当我尝试拉取数据时,它显示为空,尽管我在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:3248b和title = null bullet:3248b
我尝试了许多不同的方法来提取数据:
String date = snapshot.getValue(Bullet.Class).getDate();
String textBullet = snapshot.getValue(Bullet.Class).toString();但每次出现时,它都是空的,或者具有无法提取的全部项目符号数据。
发布于 2017-03-22 22:19:53
您在每个推流ID下都有一个名为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();但更好的解决方案是更改数据结构,使其不具有bullet级别。如果你没有在你的代码中使用它,那么在你的数据库中就没有用了。
发布于 2017-03-22 22:08:52
您的数据库引用指向Bullet,它的值为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());
}
});
}发布于 2017-09-19 18:18:21
根据DataSnapshot文档:
将DataSnapshots传递给使用addValueEventListener(ValueEventListener)、addChildEventListener(ChildEventListener)或addListenerForSingleValueEvent(ValueEventListener)附加的侦听器中的方法。
https://stackoverflow.com/questions/42950532
复制相似问题