[英]How to retrieve data with Firebase Realtime database
I want to get data that I stored on my realtime database. 我想获取存储在实时数据库中的数据。
With breakpoint, I can see that my dataSnapshot
contains the good values. 使用断点,我可以看到我的dataSnapshot
包含良好的值。 Then I want to instantiate a line object from the dataSnapshot
(Line class contains x1
, y1
, x2
and y2
fields). 然后,我想从dataSnapshot
实例化一个线对象(线类包含x1
, y1
, x2
和y2
字段)。
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (isDrawer()) {
return;
}
Line line = dataSnapshot.getValue(Line.class);
if (line != null) {
Log.i("cc", "Values : " + line.getX1() + " " + line.getX2() + " " + line.getY1() + " " + line.getY2());
addLine(line);
}
}
My problem is that the line object I get contains x1 = 0, x2 = 0, y1 = 0, y2 = 0
instead of same coordinate as the snapshot. 我的问题是我得到的线对象包含x1 = 0, x2 = 0, y1 = 0, y2 = 0
而不是与快照相同的坐标。
Edit : My current implementation of line is quite basic : 编辑:我当前行的实现是相当基本的:
public class Line {
private float x1;
private float y1;
private float x2;
private float y2;
public Line() {
}
public Line(float x1, float y1, float x2, float y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public Line(Line line) {
this.x1 = line.getX1();
this.x2 = line.getX2();
this.y1 = line.getY1();
this.y2 = line.getY2();
}
public float getX1() {
return x1;
}
public void setX1(float x1) {
this.x1 = x1;
}
public float getY1() {
return y1;
}
public void setY1(float y1) {
this.y1 = y1;
}
public float getX2() {
return x2;
}
public void setX2(float x2) {
this.x2 = x2;
}
public float getY2() {
return y2;
}
public void setY2(float y2) {
this.y2 = y2;
}
I create my Database reference the following way : 我通过以下方式创建数据库引用:
databaseReference = database.getReference("draws/unique_draw");
And I store my data like this : 我这样存储我的数据:
databaseReference.push().setValue(line);
@Alex Mamo Here is a preview of my data (lines) @Alex Mamo 这是我的数据的预览(行)
Why? 为什么?
you need to access the individual items with .getChildren()
: 您需要使用.getChildren()
访问单个项:
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot: dataSnapshot.getChildren()) {
Line line = dataSnapshot.getValue(Line.class);
if (line != null) {
Log.i("cc", "Values : " + line.getX1() + " " + line.getX2() + " " + line.getY1() + " " + line.getY2());
addLine(line);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.