0

I have this in the database:

enter image description here

When a teacher adds a Class and a Section a random id is generated using push() under node Class. There is also a Student and a Teacher node with ids and attributes.

The student joins a class, thus also creating a random id in ClassStudent with the above attribute.

In node Student the id is the current userid and under it there is attribute. Now my question is, is it a good way to retrieve the name from the Student node and add it to the ClassStudent node?

Im using this code:

DatabaseReference gets=FirebaseDatabase.getInstance().getReference().child("Student");
final DatabaseReference getid=FirebaseDatabase.getInstance().getReference().child("Class");
ValueEventListener valueEventListener1= new ValueEventListener() {
               @Override
               public void onDataChange(DataSnapshot dataSnapshot) {
                   for(DataSnapshot child: dataSnapshot.getChildren()) {
                       String classnames = child.child("Classname").getValue().toString();
                       if (returnString.equals(classnames)) {
                           String getids = child.getKey();
                           newtable.child("ClassId").setValue(getids);
                       }
                   }
               }
               @Override
               public void onCancelled(DatabaseError databaseError) {
               }
           };
           getid.addValueEventListener(valueEventListener1);

The reason I have a name because later on I want to click on a class and get the list of names in that class. So if i add the Student id(which is done already) in node ClassStudent, how will I be able to on click retrieve the name of the Students without having studentname in ClassStudent?

Edit:

enter image description here

If I query on ClassStudent , I want to retrieve the class names in one activity then on another activity, I want to retrieve the Students names of a class. I dont want Studentname to be inside ClassStudent since logically I think it seems incorrect.

Example: 1. Student registers himself in a class (a random id with attribute classid and studentid is created).

  1. Then I want to retrieve the classnames in onResume(), so after login the classes are there in activity(like a join between ClassStudent and Class`)

  2. On completely different activity, I want to retrieve the studentnames in a list while also querying on ClassStudent (like a join between ClassStudent and Student)

Output of # 3. after querying:

Peter Haddad
John
Phillip
//names
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134

1 Answers1

1

To get all the stundet names under the ClassStudent node, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference classStudentRef = rootRef.child("ClassStudent");
ValueEventListener eventListener = 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) {}
};
classStudentRef.addListenerForSingleValueEvent(eventListener);

Your out out will be:

Peter Haddad
//other names
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I mean without having `Studentname` inside node ClassStudent, I only want the studentid inside ClassStudent, so I have to do a join, if you read the last sentence in the question again. I say without having `Studentname` inside `ClassStudent` – Peter Haddad Oct 31 '17 at 07:25
  • I don't understand you entirely. There is no join when it comes to Firebase. Give me please an example about what data would you like to get and based on what? You can change your database structure with another that contains more data and then just show me de desired data. What would you like to have in your output? – Alex Mamo Oct 31 '17 at 07:32
  • basically this: https://stackoverflow.com/questions/41135658/how-to-perform-join-query-in-firebase, I tried it yesterday but got an error not related to firebase , later today I will try to fix it. – Peter Haddad Oct 31 '17 at 07:33
  • Ok. If you don't succeed, just answer my last questions and update your database structure. – Alex Mamo Oct 31 '17 at 07:37