Reputation: 47
I'm trying to retrive data as in the next photo
and here's what o got from Firebase Documentation
mCoursesChild.child("Courses").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
Course temp = data.getValue(Course.class);
courses.add(temp));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
and "courses" is defined as ArrayList
ArrayList<Course> courses = new ArrayList<>();
and here's the Course class
public class Course {
private String mCourseName;
List<Subject> mSubjects;
public Course(){} // Default constructor required for calls to DataSnapshot.getValue(Course.class)
public Course(String CourseName){
mCourseName = CourseName;
}
public String getcourseName(){
return mCourseName;
}
public List<Subject> getSubjects(){ return mSubjects; }
}
Obviously there's something wrong, So what is the value of data.getValue(Course.class) returns ? Or am I writing the database in a wrong form ?
PS : I'm writing the database objects manually
Upvotes: 1
Views: 2042
Reputation: 600126
I see a few mismatches in your code vs the naming of the JSON properties. Firebase follow JavaBean property naming conventions, which means that this getter:
public List<Subject> getSubjects(){ return mSubjects; }
Maps to a property named subjects
. In your JSON the property is named Subjects
, which does not match. To fix this problem, either spell subjects
in your JSON or annotate the getter:
@PropertyName("Subjects")
public List<Subject> getSubjects(){ return mSubjects; }
Aside from that, as Anas commented: the Firebase documentation and experts recommend keeping your data structures flat by limiting each branch of your JSON to a single type.
In your case that means I'd recommend keeping three separate top-level lists:
If you use the same key for the lessons, you still associate the lessons with a course:
Courses
CourseId1
Name: "Algebra"
Lessons
CourseId1
LessonId1
Name: "Algebra week 1"
This will make it much easier to:
Upvotes: 3