Reputation: 972
I am trying to read the following data structure
{
"posts" : {
"-KroPYVVmlkRXjiX1FhD" : {
"appliedUsers" : {
"flaged" : false,
"latitude" : 0,
"longitude" : 0,
"noFlags" : 0,
"noPosts" : 0,
"rating" : 0
},
"salary" : "5454564",
"title" : "زسروس",
}
},
"users" : {
"########" : {
"birthDate" : "8-18-1995",
"flaged" : false,
"image" : "https://scontent.xx.fbcdn.net/v/t1.0-1/p100x100/11018975_10153075514031013_8079824273778693128_n.jpg?oh=7d4a511abf155a65cda6dde4bfe7b180&oe=59F52E95",
"latitude" : 30.784419,
"longitude" : 30.987274,
"name" : "Aya Mohamed Hamed",
"noFlags" : 0,
"noPosts" : 0,
"rating" : 0,
"uid" : "########",
"userPhone" : "01007211319"
}
}
}
using the following POJO
public class Job {
public Map<String, User> appliedUsers = new HashMap<>();
private String title;
private String salary;
// empty public constructor
// another constructor
// public getters and setters per each property
and the User POJO class
public class User {
private String uid;
private String image;
private String name;
private String birthDate;
private String userPhone;
private long rating;
private int noPosts;
private int noFlags;
private String address;
private double longitude;
private double latitude;
private boolean isFlaged;
// empty public constructor
// another constructor
// public getters and setters per each property}
My Read Jobs function is:
public static void getAllJobs(JobsDataChangeListener jobsDataChangeListener) {
postsDBRef = FirebaseDatabase.getInstance().getReference("posts");
postsDBRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
ArrayList<Job> jobs = new ArrayList<>();
Logging.log(""+dataSnapshot.getValue());
Job job = dataSnapshot.getValue(Job.class);
jobs.add(job);
jobsDataChangeListener.onJobsDataChange(jobs, 1);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.i(getClass().getName().toString(), ": " + databaseError.getMessage());
}
});
}
The function was retrieving posts fine until I inserted the applied users Map to the Pojo object, each time I try to read the jobs/posts, I get the following issue
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Long to type com.jobease.www.jobease.models.User
Upvotes: 2
Views: 311
Reputation: 599166
The data under appliedUsers
does not match your definition in code.
appliedUsers
flaged: false
latitude: 0
In your code you're mapping this to:
public Map<String, User> appliedUsers = new HashMap<>();
You didn't share the definition of User
, but it seems unlikely that it's a type that allows both a boolean
(for flaged
) and a number (for latitude
).
When you're trying to read the job, the client throws an error because it can't convert 0
to a User
object.
Update
If what you have under appliedUsers
is a single user, your definition should be:
public class Job {
public User appliedUsers;
private String title;
private String salary;
...
Upvotes: 1
Reputation: 440
Try this:
FirebaseDatabase.getInstance()
.getReference()
.child("posts")
.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Job job = dataSnapshot.getValue(Job.class);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Upvotes: 0