I am new to Firebase database and my data structure in Firebase is like this:
Root- users-sec_a, sec_b, sec_c
In each section(sec_a, sec_b, sec_c) there will be uids of users. in each section there is min of 40 uids. And in all uids there is a common data child's (name , rollno , attendance,) Now my question was I want to display only names and rollno of all uids present in one of sections(section to be displayed was inputted by user).
How can I get that data?
it's fairly easy.
String sectionSelectedByUser = "sec_a"; // For example
Now getting refrence of the firebase database
if (FirebaseAuth.getInstance().getCurrentUser() != null){ // if you need user to be signed in..
FirebaseDatabase.getInstance().getReference().child("users").child(sectionSelectedByUser).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) { // iterates through all your UID in this section
if (childSnapshot.hasChild("name")){ // if current uid has name then fetch it
String name = childSnapshot.child("name").getValue().toString();
}
if (childSnapshot.hasChild("rollno")){ // if current UID has rollno then fetch it
String rollno = childSnapshot.child("rollno").getValue().toString();
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
You can retrieve nested data with child method of Fire base database.
For you problem you can use this code with little modification as per requirements.
ValueEventListener singleEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot != null) {
Student student = dataSnapshot.getValue(Student.class);
Log.d("TAG","name: "+student.getName());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("onCancelled - ", databaseError.toException());
}
};
if (mFirebaseDatabaseReference == null) {
mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
//mFirebaseDatabaseReference.keepSynced(true);
}
mFirebaseDatabaseReference = mFirebaseDatabaseReference.child(Constant_String.Table_User);
mFirebaseDatabaseReference.addListenerForSingleValueEvent(singleEventListener);
Hope this will help...
Assuming that your database schema looks like this:
Firebase-root
|
--- users
|
--- sec_a
| |
| --- uid
| | |
| | --- rollno: "Roll Number"
| | |
| | --- name: "User Name"
| | |
| | --- attendance: true
| |
| --- //other 40 users
|
--- sec_b
|
--- sec_c
And if you want for example to display only names and rollno of all uids present in one of sections (eg sec_a
), please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference sectionRef = rootRef.child("users").child("sec_a");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String name = ds.child("name").getValue(String.class);
String rollno = ds.child("rollno").getValue(String.class);
Log.d(TAG, name + " / " + rollno);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
}
};
sectionRef.addListenerForSingleValueEvent(valueEventListener);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.