I want to get data that I stored on my realtime database.
With breakpoint, I can see that my dataSnapshot
contains the good values. Then I want to instantiate a line object from the dataSnapshot
(Line class contains x1
, y1
, x2
and y2
fields).
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.
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)
Why?
you need to access the individual items with .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);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.