How will the project Structure look like

2020-05-23 android firebase-realtime-database

How will the project structure look like on FirebaseDatabase.i got it from an open source code but i cant figure it out how will this look like

FirebaseDatabase database = FirebaseDatabase.getInstance();
Query dbQuery = database.getReference("teachers-courses").orderByChild("teacherNo").equalTo(uid);

and this

dbQuery.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot courseSnapshot: dataSnapshot.getChildren()) {
            String course = courseSnapshot.child("courseNo").getValue().toString();
            courses.add(course);
        }

        // Resolve query for courses names.
        Query coursesQuery = database.getReference("courses");

        // Event listenere to update.
        coursesQuery.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot courseSnapshot: dataSnapshot.getChildren()) {
                    // Grab the key.
                    String key = courseSnapshot.getKey();
                    // If key is required.
                    if (courses.contains(key)){
                        String courseName = courseSnapshot.child("name").getValue().toString();
                        coursesName.add(courseName);
                    }
                }

                // Fill the list.
                ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                        CourseTeacherActivity.this,
                        android.R.layout.simple_list_item_1,
                        coursesName );

                // Set the adapter.
                lv.setAdapter(arrayAdapter);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                System.out.println("The read failed: " + databaseError.getCode());

            }
        });
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        System.out.println("The read failed: " + databaseError.getCode());
    }
});

this is a source code from github and was trying to figure it out for days now but i cant seem to figure it out i will appreciate if you can show me what the project structure would look like in Firebase Database thank you in advance

Answers

The code you shared seems to load the courses for a specific teacher, but first loading the course IDs for that teacher, and then loading the course data.

Let's see what we can determine about the JSON structure from the code:

  1. The database.getReference("teachers-courses").orderByChild("teacherNo").equalTo(uid) indicates:

    1. That there is a child teachers-courses under the root.
    2. That each child node under there has a property teacherNo with a UID as its value.

    3. This typically means that the nodes directly under teachers-courses use a push ID as their key.

  2. The courseSnapshot.child("courseNo").getValue().toString() then shows that each child node has a courseNo property.

  3. The database.getReference("courses") shows that there's another top-level node courses that contains the courses. At this point it becomes clear that we have a fairly standard many-to-many relationship, as for example explained here: Many to Many relationship in Firebase

  4. The courseSnapshot.getKey(); indicates that the key for each node under /courses is the ID for that course.

  5. The courseSnapshot.child("name").getValue().toString() shows that each course has a name property.

All of this combined brings us to the following JSON:

"teachers-courses": { // 1.1
  "somepushid": { // 1.3
    "teacherNo": "someteacherid" // 1.2
    "courseNo": "somecourseid" // 2
  }
},
"courses": { // 3
  "somecourseid": { // 4
    "name": "The name of the course" // 5
  }
}

There is likely to be more data in the database, but this is what is being used by the code you shared.

Related