Reputation:
When I try to get value like this:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("positions");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Object userPosition = ds.getValue();
System.out.println("abc " + userPosition);
}
}
@Override
public void onCancelled(DatabaseError error) {
}
});
everything works fine but when I try to get value like this:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("positions");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
UserPosition userPosition = ds.getValue(UserPosition.class);
System.out.println("abc " + userPosition);
}
}
@Override
public void onCancelled(DatabaseError error) {
}
});
I get no value (System.out.println() not displays at all).
UserPosition.class
public class UserPosition {
private String currentMap;
private boolean logged;
private String login;
private int x, y;
public String getCurrentMap() {
return currentMap;
}
public void setCurrentMap(String currentMap) {
this.currentMap = currentMap;
}
public boolean isLogged() {
return logged;
}
public void setLogged(boolean logged) {
this.logged = logged;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public UserPosition(String currentMap, boolean logged, String login, int x, int y) {
this.login = login;
this.currentMap = currentMap;
this.x = x;
this.y = y;
this.logged = logged;
}
}
Why it happens and how to solve that?
If you need something more just ask. I do not know what else I should add because I cannot post my question.
Thank you in advance!
Upvotes: 0
Views: 47
Reputation: 8383
Your class does not have a default (NO argument constructor), you will need to provide that.
According to official documentation, you need the following in order to be able to marshal the value into an Object of type .Class.
public T getValue (Class valueType)
This method is used to marshall the data contained in this snapshot into a class of your choosing. The class must fit 2 simple constraints:
The class must have a default constructor that takes no arguments The class must define public getters for the properties to be assigned. Properties without a public getter will be set to their default value when an instance is deserialized
Upvotes: 1