Firebase Queries - android

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 between ClassStudent and Class`)
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

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

Related

Search firebase database for key and check if it contains expected value

I want to use the user id of the user object to check if the role value is equal to employee, but I am not sure how to do that. I know how to get the user id but the rest i am unaware of. My database looks like this:
-User
-userid
firstName:John
lastName:Wall
role:Employee
- userid
firstName:Bradley
lastName:Beal
role:Patient
You need to do a query:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("User");
ref.orderByChild("role").equalTo("Employee").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
String name = ds.child("firstName").getValue(String.class);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
First add reference to node User then use orderByChild() to retrieve the name of the users that have role equal to Employee.

How can I set database reference to child of child of order in mentioned image ? Or how can i get the value vehicle name

This my FireBase structure. how can I make database reference set to a child of a child of order or get the value of vehicle name, from location and to location and display it in card view Recyclerview?
Assuming that the ved_nam property is the vehicle name and the order node is a direct child of your Firebase root, to log all your vehicle names, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference orderIdRef = rootRef.child("order").child(orderId);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String ved_nam = ds.child("ved_nam").getValue(String.class);
Log.d("TAG", ved_nam);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
orderIdRef.addListenerForSingleValueEvent(valueEventListener);
In which the orderId is the id of the order that you are looking for (i.e. ACSIenh9E6WYiLJWAyn1icsHlzr2).

How can i retrieve a specific data from nodes in firebase and display to lisltview

I already have knowledge in retrieving data from firebase. The thing is, the data i want to retrieve lies under 3 parent nodes, and I can't figure any way how to retrieve data from the database 3 nodes deep. What I want is to get all the values of the key named "name" and display it in a listview.
To display all those names, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference medicinesRef = rootRef.child("Medicines");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String name = ds.child("name").getValue(String.class);
Log.d("TAG", name);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
medicinesRef.addListenerForSingleValueEvent(eventListener);
Your output will be:
amoxcillin
paracetamol
mefenamic

android firebase database: how to get all nodes that are in a set of values

I am trying to retrieve a list of friends for a user so I can display them in a list view. The friends and user info is structured like this in my firebase database:
So basically, I want to take the user ids listed in the friends part and query my users data to get all the info under every node that is in that set of user ids. How can I achieve this using the android firebase database sdk querying? I would like to be able to retrieve all the users in a single database query.
Thanks.
You cannot get that data in single query, you need to query your database twice. This is a common practice when it comes to Firebase. Assuming that friends and users nodes are direct childs of your Firebase root, to achieve this, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference friendIdRef = rootRef.child("friends").child(friendId)
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String key = ds.getKey();
DatabaseReference usersRef = rootRef.child("users").child(key);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
String username = dSnapshot.child("username").getValue(String.class);
Log.d("TAG", username);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
usersRef.addListenerForSingleValueEvent(eventListener);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
friendIdRef.addListenerForSingleValueEvent(valueEventListener);
It will print all user names of those particular users. One more thing to note, is that you don't need to add in your database those friends that have the value of false, only those with the value of true.

Getting child values in Firebase

I have the following structure
root
-class1
-section1
-student1
-firstname
-lastname
-student2
-firstname
-lastname
-section2
-student1
-firstname
-lastname
-student2
-firstname
-lastname
I need to get the section number (Section1, section2...) and for every section, the lastname for student2.
The problem that i facing is that it is all in a Fragment class in android that I need to display and nothing seems to be working. I have referred this for getting the children too, but the for loop somehow does not work and I get Null values on retrieval.
I need to get
section1, lastname (of student2 in section1)
section2, lastname (of student2 in section2)
...
and so on until all sections are exhausted
U can make a model class like
class Class{
Arrylist<section> sections;
};
class section{
Arrylist<Student> students;
};
class Student{
String firstname, lastname;
};
then get the value of classes
have other way, without making many model classes, but that is more painful and time consuming . If u want that feel free to comment.
Please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference classRef = rootRef.child("class1");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String lastname = ds.child("student2").child("lastname").getValue(String.class);
Log.d("TAG", lastname);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
classRef.addListenerForSingleValueEvent(eventListener);

Categories

Resources