Reputation: 13
First time working with Firebase realtime database I'm trying to read data from the database with Android application this is my first experience I'm sorry I'm used to working with MySQL so this JSON format seem to be complicated for me
I get the error No setter/field for found on class.
private FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
DatabaseReference mDbRef = mDatabase.getReference().child("users");
mDbRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Users user = dataSnapshot.getValue(Users.class);
Log.d("result", "User name: " + user.getName() + ", email " + user.getEmail());
}
@Override
public void onCancelled(DatabaseError error) {
Log.w("error", "Failed to read value.", error.toException());
}
});
and this my Users class
public class Users {
private String phone;
private String email;
private String name;
private String adresse;
private String ville;
private String zip;
public Users() {
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAdresse() {
return adresse;
}
public void setAdresse(String adresse) {
this.adresse = adresse;
}
public String getVille() {
return ville;
}
public void setVille(String ville) {
this.ville = ville;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
}
I have attached an image for the struct of the database if anyone have any solution please help me
Upvotes: 1
Views: 817
Reputation: 351
You should use ChildEventListener as your data is a list , if u use ValueEventListener then it will return the entire list of data as a single DataSnapshot which is not the recommended way.
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("users");
ChildEventListener childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
Users user = dataSnapshot.getValue(Users.class);
Log.d("result", "User name: " + user.getName() + ", email " + user.getEmail());
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
usersRef.addChildEventListener(childEventListener);
Upvotes: 1
Reputation: 138869
To solve this problem, you need to loop through entire users
node using getChildren()
method, like in the following lines of code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("users");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Users users = ds.getValue(Users.class);
Log.d("result", "User name: " + user.getName() + ", email " + user.getEmail());
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
usersRef.addListenerForSingleValueEvent(valueEventListener);
The result in your logcat will be the name and email of all users.
Upvotes: 1