I am writing an Android app and I am trying to retrieve an object of the class "User.java" by ID from its Firebase pertinent table. I can't find what I am doing wrong; can someone help me with this? I'll be very grateful. Thanks.
This is the error I am getting:
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Double to type com.example.nidailyas.fitme.User
My code:
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot userDetails : dataSnapshot.getChildren()) {
User user = userDetails.getValue(User.class);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
User.java
import java.util.Date;
public class User {
String userId; //PK
String name;
Date dateOfBirth;
String gender;
double weight;
double height;
double begin_bp_upper;
double getBegin_bp_lower;
int score;
String planningId; //FK
String levelId; //FK
String profileImageUrl;
public User() {
}
public User(String userId, String name, Date dateOfBirth, String gender, double weight, double height, double begin_bp_upper, double getBegin_bp_lower, int score, String planningId, String levelId, String profileImageUrl) {
this.userId = userId;
this.name = name;
this.dateOfBirth = dateOfBirth;
this.gender = gender;
this.weight = weight;
this.height = height;
this.begin_bp_upper = begin_bp_upper;
this.getBegin_bp_lower = getBegin_bp_lower;
this.score = score;
this.planningId = planningId;
this.levelId = levelId;
this.profileImageUrl = profileImageUrl;
}
public String getProfileImageUrl() {
return profileImageUrl;
}
public String getUserId() {
return userId;
}
public int getScore() {
return score;
}
public String getPlanningId() {
return planningId;
}
public String getLevelId() {
return levelId;
}
public double getBegin_bp_upper() {
return begin_bp_upper;
}
public double getGetBegin_bp_lower() {
return getBegin_bp_lower;
}
public String getName() {
return name;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
public String getGender() {
return gender;
}
public double getWeight() {
return weight;
}
public double getHeight() {
return height;
}
}
By using the database reference:
mDatabase.child("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid())
You're calling for users/{userId}
value which should return a single user object from the database. (This is true considering how you design your data schema)
The onDataChange
argument dataSnapshot
is returning the user object, so, when you're calling dataSnapshot.getChildren()
your asking for the children of the user object, in this particular case, that turns to be the properties of the user (begin_bp_upper, dateOfBirth, gender, ....).
Making a wild guess, i assume the exception thrown is because User user = userDetails.getValue(User.class);
is trying to convert the value of begin_bp_upper
(which is a Double) to type User
.
So, instead of using that for
statement, consider dataSnapshot
as the user object:
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
User user = dataSnapshot.getValue(User.class);
}
}
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.