Reputation: 312
I have this structure:
And I need to get the data from "OnlineClients". My attempt:
currentDriverId = mFireBaseAuth.getCurrentUser().getUid();
mDataBase = FirebaseDatabase.getInstance().getReference().child("Users").child("Driver").child(currentDriverId).child("OnlineClients");
final ArrayList<Student> users = new ArrayList<>();
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
Student student = data.getValue(Student.class);
users.add(student);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
}
};
mDataBase.addListenerForSingleValueEvent(postListener);
with error:
com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String
Student.Class:
public class Student {
private String FirstName;
private String LastName;
private String StudentSchool;
private String StudentState;
private String StudentStreet;
private String StudentStreetNumber;
public Student(){
}
public String getFirstName() {
return FirstName;
}
public void setFirstName(String firstName) {
FirstName = firstName;
}
public String getLastName() {
return LastName;
}
public void setLastName(String lastName) {
LastName = lastName;
}
public String getStudentSchool() {
return StudentSchool;
}
public void setStudentSchool(String studentSchool) {
StudentSchool = studentSchool;
}
public String getStudentState() {
return StudentState;
}
public void setStudentState(String studentState) {
StudentState = studentState;
}
public String getStudentStreet() {
return StudentStreet;
}
public void setStudentStreet(String studentStreet) {
StudentStreet = studentStreet;
}
public String getStudentStreetNumber() {
return StudentStreetNumber;
}
public void setStudentStreetNumber(String studentStreetNumber) {
StudentStreetNumber = studentStreetNumber;
}
}
Already tried: how to retrieve all the data within nested firebase database and similar without success.
Note: I only need to retrieve "OnlineClients" from current logged user (that's why I'm just using currentDriverId).
Upvotes: 0
Views: 89
Reputation: 138824
The problem in your code lies in the fact that your StudentStreetNumber
field is declared in your Student
class of type String
while in your database is a number (long). To solve this, simply change the type of your property to long
:
private long StudentStreetNumber;
Another problem in your code are the getters. To solve this problem too, please see my answer from the following post:
Upvotes: 0
Reputation: 80914
Change this:
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
Student student = data.getValue(Student.class);
users.add(student);
}
}
into this:
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Student student = dataSnapshot.getValue(Student.class);
users.add(student);
}
Remove the for loop because when you are looping, you are retrieving the values as String
. Also make sure when you use setValue()
you are adding an object of Type Student
as an argument.
Another way to retrieve the values is to do the following:
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
String firstName = data.child("FirstName").getValue(String.class);
String lastName = data.child("LastName").getValue(String.class);
}
}
Upvotes: 1