I have this in the database:
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:
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).
Then I want to retrieve the classnames in
onResume()
, so after login the classes are there in activity(like a join betweenClassStudent
and Class`)On completely different activity, I want to retrieve the
studentnames
in a list while also querying onClassStudent
(like a join betweenClassStudent
andStudent
)
Output of # 3. after querying:
Peter Haddad
John
Phillip
//names