[英]How to Fetch child nodes without using unique ids using Query from Firebase database in Android Studio?
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.我在 Android Studio 中设置了一个RecyclerAdater
,我需要将我的数据库指向节点 Courses 以获取所有子节点(CourseDate,CourseId,...),这些子节点在节点Courses
有 2 种深度,而无需使用查询知道它们的唯一 ID在火力基地。 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
,如下所示;
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在节点课程中有 2 种深度
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:为了解决这个问题,您需要循环遍历Courses
节点两次,为此,我建议您使用以下代码行:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.