我正在开发 Android 应用程序Firebase
用于维护移动后端。我有这种格式的数据库Firebase
{
"users" : {
"-Kh2wjxeT-w26opA0yqe" : {
"email" : "[email protected] /cdn-cgi/l/email-protection",
"firstName" : "XX",
"interest" : {
"interests" : [ "Cricket", "Table Tennis", "Football" ]
},
"lastName" : "XXX",
"password" : "abcd@1234"
}
}
}
用户.java
@IgnoreExtraProperties
public class User {
public String firstName;
public String lastName;
public String email;
public String password;
public Interest interest;
public User() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}
public User(String firstName, String lastName, String email, String password, Interest interest) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.password = password;
this.interest = interest;
}
}
兴趣.java
public class Interest {
public List<String> interests;
public Interest() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}
public Interest(List<String> interests) {
this.interests = interests;
}
}
我已经添加了addValueEventListener
以这种方式对儿童“用户”
mDatabase.child("users").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot noteDataSnapshot : dataSnapshot.getChildren()) {
User user = dataSnapshot.getValue(User.class);
Log.d("Scheduled", user.firstName + user.lastName + user.interest.interests.toString());
}
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
}
});
它正在获取我所拥有的正确数据Firebase
但它没有映射数据Users
java 对象,所以当我无法访问值时。我不确定我在这里犯了什么错误。
Log.d("已安排", user.firstName + user.lastName + user.interest.interests.toString());