简体   繁体   English

就我而言,哪种Firebase数据结构行为最佳?

[英]Which Firebase data structure behaviour is best in my case?

We are creating an app where the teacher can create his class register, and it should look like that: Teacher creates his profile, after that there is an activity with RecyclerView that displays students names. 我们正在创建一个应用程序,教师可以在其中创建班级注册,看起来应该是这样的:老师创建了自己的个人资料,然后有一个RecyclerView活动显示了学生的姓名。 Below RecyclerView there is TextView that says "Student! create your own profile" (ofcourse after clicking it, there is an activity for creating students profile). RecyclerView下方,有一个TextView ,上面写着“学生!创建自己的个人资料”(当然,单击它后,会有一个用于创建学生个人资料的活动)。 Student can create his profile only by using the same device as teacher while he was creating Teachers Profile. 学生只能在创建教师档案时使用与教师相同的设备来创建档案。 Ok, that looks nice and so far we have it all done, but here comes our question. 好的,这看起来不错,到目前为止,我们已经完成了所有工作,但是这里出现了我们的问题。 What is the best behaviour to store users data in this case? 在这种情况下,存储用户数据的最佳行为是什么? Should we structure our data this way? 我们应该这样构造数据吗?

{
  "Teachers" : {
    "USYSacnOjDR5EAPwljZMHtggN9I2" : {
      "teachername" : {
        "teachername" : "Janis"
      },
        "students" : {
          "0xgMzfOLLwQ2KWF7aKhH5ZIbQnx2":{
            "studentname": "Pavel"

         }
      }
    }
  },

So, this looks like veeery badly structured data, we know it, but like we said before, We need to display in RecyclerView the names of this specific teacher students that are other FirebaseAuth users and had their own datas like grades Every opinion or critique is appreciated. 因此,我们知道它看起来像是veerery数据结构不良,但正如我们之前所说,我们需要在RecyclerView显示该特定教师学生的姓名,这些学生是FirebaseAuth的其他用户,并且拥有自己的数据,例如成绩。赞赏。

@Edit here is how i retrieve data :) reference=FirebaseDatabase.getInstance().getReference("teachers").child(teacherkey).child("users"); @Edit是我检索数据的方式:) reference = FirebaseDatabase.getInstance()。getReference(“ teachers”)。child(teacherkey).child(“ users”);

 reference.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                User user = dataSnapshot.child("name").getValue(User.class);
                result.add(user);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

                User user = dataSnapshot.child("name").getValue(User.class);
                result.remove(user);
                adapter.notifyDataSetChanged();
            }

According to your comment, a possible database structure can be this: 根据您的评论,可能的数据库结构可以是:

Firebase-root
   |
   --- teachers
   |     |
   |     --- teacherIdOne
   |     |      |
   |     |      --- "teacherName" : "Teacher Name One"
   |     |      |
   |     |      --- students:
   |     |             |
   |     |             --- studentIdOne: "Student Name One"
   |     |             |
   |     |             --- studentIdTwo: "Student Name Two"
   |     |
   |     --- teacherIdTwo
   |            |
   |            --- "teacherName" : "Teacher Name Two"
   |            |
   |            --- students:
   |                   |
   |                   --- studentIdThree: "Student Name Three"
   |                   |
   |                   --- studentIdFour: "Student Name Four"
   |
   --- students
         |
         --- studentIdOne
         |      |
         |      --- "studentName" : "Student Name One"
         |
         --- studentIdTwo
         |      |
         |      --- "studentName" : "Student Name Two"
         |
         --- studentIdThree
         |      |
         |      --- "studentName" : "Student Name Three"
         |
         --- studentIdFour
         |      |
         |      --- "studentName" : "Student Name Four"
         |
         --- studentIdFive
                |
                --- "studentName" : "Student Name Five"

And to display all 5 students name, please use the following code: 要显示所有5名学生的姓名,请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference studentsRef = rootRef.child("students");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String studentName = ds.child("studentName").getValue(String.class);
            Log.d("TAG", studentName);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
studentsRef.addListenerForSingleValueEvent(valueEventListener);

Te output will be: Te输出将是:

Student Name One
Student Name Two
Student Name Three
Student Name Four
Student Name Five

If you are interested in displaying those names into a RecyclerView , this is how you can retrieve data from a Firebase Realtime database and display it in a RecyclerView using FirebaseRecyclerAdapter . 如果您有兴趣在RecyclerView中显示这些名称,则可以使用这种方法从Firebase Realtime数据库检索数据,并使用FirebaseRecyclerAdapterRecyclerView显示数据。

If you want to display the student of a particular teacher, please use the following code: 如果要显示特定老师的学生,请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference studentsRef = rootRef.child(teachers).child(teacherId).child("students");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String studentName = ds.getValue();
            Log.d("TAG", studentName);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
studentsRef.addListenerForSingleValueEvent(valueEventListener);

In which the teacherId is the id of the teacher you want to display its students. 其中teacherId是要显示其学生的教师的ID。 If the id of the teacher is for example teacherIdOne , the output will be: 例如,如果教师的id为teacherIdOne ,则输出为:

Student Name One
Student Name Two

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM