问题
I'm still getting an exception:
com.firebase.client.FirebaseException: Failed to bounce to type exception
for following line
Peoples people = dataSnapshot.getValue(Peoples.class);
Peoples object
public class Peoples {
private String ambition;
private String fname;
private String lname;
private String emailid;
Peoples(){
}
Peoples(String ambition,String fname,String lname,String emailid){
this.ambition=ambition;
this.fname=fname;
this.lname=lname;
this.emailid=emailid;
}
public String getAmbition() {
return ambition;
}
public String getFname() {
return fname;
}
public String getLname() {
return lname;
}
public String getEmailid() {
return emailid;
}
}
Data
peoples
-K3yJU5z646EZiIliPuJ
ambition: "Doctor"
emailid: "harshan@gmail.com"
fname: "Mohammed"
lname: "Thanish"
-K3yJcoLNqY228wQO1AX
ambition: "Doctor"
emailid: "ndlhassan@gmail.com"
fname: "Mohammed"
lname: "Thanish"
Code
userRef
.orderByChild("emailid")
.equalTo("ndlhassan@gmail.com")
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println("USERRRRRRRRRRRRRRRRRRR are " + dataSnapshot.getChildrenCount() + " blog posts");
System.out.println("USERRRRRRRRRRRRRRRRRRR" + dataSnapshot.getValue());
Peoples people = dataSnapshot.getValue(Peoples.class);
System.out.println("USERRRRRRRRRRRRRRRRRRR" + people.getEmailid());
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
回答1:
Since you're listening to a value event, the onDataChange method can potentially be called with multiple matching items. Even though in your case there is only one match, your code will need to be prepared to handle multiple:
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println("There are " + dataSnapshot.getChildrenCount() + " children");
for (DataSnapshot child: dataSnapshot.getChildren()) {
Peoples people = child.getValue(Peoples.class);
System.out.println("Child: " + people.getEmailid());
}
}
Alternatively, you can add a ChildEventListener. https://www.firebase.com/docs/android/guide/retrieving-data.html
来源:https://stackoverflow.com/questions/33921727/firebaseexception-failed-to-bounce-to-type-in-android-but-model-class-propertie