I have set a RecyclerAdater
in Android Studio and I need to point my database to the node Courses to fetch all the child nodes (CourseDate,CourseId,...) which are 2 way deep in the node Courses
without knowing their unique ids using query in firebase. So how can i do it?
Query query= (Query) CourseDatabaseRefer.addChildEventListener( new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
dataSnapshot.getChildren();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
dataSnapshot.getChildren();
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
} );
That is what I am trying to do so that after that I can pass the query object to my FirebaseRecyclerOptions
as show below;
FirebaseRecyclerOptions<AllCourses> RecyclerOption=new FirebaseRecyclerOptions.Builder<AllCourses>()
.setQuery( query,AllCourses.class ).build();
But it seems not to be working out...any help would be appreciated.
which are 2 way deep in the node Courses
To solve this, you need to loop through the Courses
node twice and for that, I recommend you to use the following lines of code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference coursesRef = rootRef.child("Courses");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<AllCourses> list = new ArrayList<>();
for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
for(DataSnapshot ds : dSnapshot.getChildren()) {
AllCourses allCourses = ds.getValue(AllCourses.class);
list.add(allCourses);
}
}
//Do what you need to do with the list
Log.d("TAG", list.size());
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore potential errors!
}
};
coursesRef.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.