Firebase 数据映射到 Java class
Firebase data mapping to Java class
我使用 Firebase 作为后端,我能够获取数据,但无法将其映射到 Java class。我也尝试使用 JSONObject 进行映射,但问题是我应该忽略键 (1),因为我是根据电子邮件值进行查询的。
数据
users
1:
email: sample@email.com
firstName: Big
lastName: Ben
groups:
instructor: true
Firebase相关代码:
Firebase ref = new Firebase("firebaseURL");
Query queryRef = ref.orderByChild("email").equalTo(mEmailEdit.getText().toString()).limitToFirst(1);
// Attach an listener to read the data at our posts reference
queryRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
User user = snapshot.getValue(User.class);
System.out.println("user : " + user);
System.out.println("children : " + snapshot.getChildren().iterator().next());
mProgressDialog.dismiss();
}
@Override
public void onCancelled(FirebaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
User.java
public class User {
private String email;
private String firstName;
private String lastName;
private Group group;
public User(){}
public User(String email, String firstName, String lastName) {
this.email = email;
this.firstName = firstName;
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return email.equals(user.email);
}
@Override
public int hashCode() {
return email.hashCode();
}
public class Group{
private boolean isInstructor;
private boolean isManager;
public Group(){}
public Group(boolean isInstructor, boolean isManager) {
this.isInstructor = isInstructor;
this.isManager = isManager;
}
public boolean isInstructor() {
return isInstructor;
}
public void setIsInstructor(boolean isInstructor) {
this.isInstructor = isInstructor;
}
public boolean isManager() {
return isManager;
}
public void setIsManager(boolean isManager) {
this.isManager = isManager;
}
@Override
public String toString() {
return "Group{" +
"isInstructor=" + isInstructor +
", isManager=" + isManager +
'}';
}
}
@Override
public String toString() {
return "User{" +
"email='" + email + '\'' +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", group=" + group +
'}';
}
}
我已经明白了。从 onDataChanged
方法,我将代码放在下面以将数据映射到用户 class。
snapshot.getChildren().iterator().next().getValue(User.class)
// snapshot.getChildren().iterator().next() // returns the child of the returned data
// .getValue(User.class) // since we already retrieved the child, and we're
// // now referring to User class data, we can now
// // map the data using Firebase method (getValue(Class<T>))
我使用 Firebase 作为后端,我能够获取数据,但无法将其映射到 Java class。我也尝试使用 JSONObject 进行映射,但问题是我应该忽略键 (1),因为我是根据电子邮件值进行查询的。
数据
users
1:
email: sample@email.com
firstName: Big
lastName: Ben
groups:
instructor: true
Firebase相关代码:
Firebase ref = new Firebase("firebaseURL");
Query queryRef = ref.orderByChild("email").equalTo(mEmailEdit.getText().toString()).limitToFirst(1);
// Attach an listener to read the data at our posts reference
queryRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
User user = snapshot.getValue(User.class);
System.out.println("user : " + user);
System.out.println("children : " + snapshot.getChildren().iterator().next());
mProgressDialog.dismiss();
}
@Override
public void onCancelled(FirebaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
User.java
public class User {
private String email;
private String firstName;
private String lastName;
private Group group;
public User(){}
public User(String email, String firstName, String lastName) {
this.email = email;
this.firstName = firstName;
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return email.equals(user.email);
}
@Override
public int hashCode() {
return email.hashCode();
}
public class Group{
private boolean isInstructor;
private boolean isManager;
public Group(){}
public Group(boolean isInstructor, boolean isManager) {
this.isInstructor = isInstructor;
this.isManager = isManager;
}
public boolean isInstructor() {
return isInstructor;
}
public void setIsInstructor(boolean isInstructor) {
this.isInstructor = isInstructor;
}
public boolean isManager() {
return isManager;
}
public void setIsManager(boolean isManager) {
this.isManager = isManager;
}
@Override
public String toString() {
return "Group{" +
"isInstructor=" + isInstructor +
", isManager=" + isManager +
'}';
}
}
@Override
public String toString() {
return "User{" +
"email='" + email + '\'' +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", group=" + group +
'}';
}
}
我已经明白了。从 onDataChanged
方法,我将代码放在下面以将数据映射到用户 class。
snapshot.getChildren().iterator().next().getValue(User.class)
// snapshot.getChildren().iterator().next() // returns the child of the returned data
// .getValue(User.class) // since we already retrieved the child, and we're
// // now referring to User class data, we can now
// // map the data using Firebase method (getValue(Class<T>))