【发布时间】:2019-03-31 09:56:53
【问题描述】:
我在尝试访问我的 firebase 实时数据库中的嵌套子节点时遇到问题。
我遇到的错误是
com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的对象转换为 sg.edu.singaporetech.teamproject.users 类型
这是我的数据库结构的样子:
我从这里看到了解决方案:How do I access the child nodes nested inside child node in Firebase database?
这是我尝试从 firebase 调用数据的代码:
DatabaseReference databaseUsers;
databaseUsers = FirebaseDatabase.getInstance().getReference("users");
databaseUsers.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//method is called whenever data at location is updated
showData(dataSnapshot);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return view;
}
private void showData(DataSnapshot dataSnapshot) {
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
String userID = currentUser.getUid();
for (DataSnapshot uniqueIDSnapshot : dataSnapshot.getChildren()) {
//Loop 1 to go through all child nodes of users
for(DataSnapshot vouchersSnapshot : uniqueIDSnapshot.child(userID).getChildren()){
//loop 2 to go through all the child nodes of vouchers node
users user = vouchersSnapshot.getValue(users.class);
}
}
}
这是来自我的users 班级
package sg.edu.singaporetech.teamproject;
public class users {
String id;
String email;
String level;
String steps;
String vouchers;
public users() {
}
public users(String id, String email, String level) {
this.id = id;
this.email = email;
this.level = level;
this.steps = steps;
this.vouchers = vouchers;
}
public String getId() {
return id;
}
public String getEmail() {
return email;
}
public String getLevel() {
return level;
}
public String getSteps() { return steps;}
public String getVouchers() { return vouchers;}
public void setId(String id) {
this.id = id;
}
public void setEmail(String email) {
this.email = email;
}
public void setLevel(String level) {
this.level = level;
}
public void setSteps(String steps) {
this.steps = steps;
}
public void setVouchers(String vouchers) {
this.vouchers = vouchers;
}
}
那么如何才能不报错,具体获取当月数据库结构中看到的“真”或“假”值,也就是march19呢?
非常感谢您的帮助。
【问题讨论】:
标签: java android firebase firebase-realtime-database