从Firebase

时间:2018-04-13 17:51:57

标签: java android database firebase

我正在编写一个Android应用程序,我正在尝试通过其Firebase相关表中的ID检索“User.java”类的对象。 我找不到我做错了什么;有人可以帮我弄这个吗?我将非常感激。感谢。

这是我得到的错误:

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Double to type com.example.nidailyas.fitme.User

我的代码:

 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;
}
}

Firebase db looks like this:

1 个答案:

答案 0 :(得分:0)

使用数据库参考:

mDatabase.child("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid())

您正在调用users/{userId}值,该值应从数据库返回单个用户对象。 (考虑到如何设计数据模式,这是真的)

onDataChange参数dataSnapshot正在返回用户对象,因此,当您在调用dataSnapshot.getChildren()时要求用户对象的子项时,在这种特殊情况下,转为用户的属性(begin_bp_upper,dateOfBirth,gender,....)。

疯狂猜测,我假设引发的异常是因为User user = userDetails.getValue(User.class);正在尝试将begin_bp_upper(这是一个Double)的值转换为User类型。

因此,不要使用for语句,而应将dataSnapshot视为用户对象:

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
         if (dataSnapshot.exists()) {
               User user = dataSnapshot.getValue(User.class);
         }
}