简体   繁体   English

如何在 Android Studio 中使用来自 Firebase 数据库的查询在不使用唯一 ID 的情况下获取子节点?

[英]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?那么我该怎么做呢?

Database picture数据库图片

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.

相关问题 如何在 android 工作室中的 firebase 数据库中读取子节点而不提及父节点? - How to read child nodes without mentioning parent node in firebase database within android studio? 在Android Studio中使用复杂的查询从Firebase数据库获取节点 - Getting nodes from Firebase Database with a complex query in Android Studio 如何在Firebase数据库Android上获取具有相似子节点的所有父节点? - How to fetch all parent nodes with similar child nodes on Firebase database Android? 如何在 android 工作室中从 firebase 检索多个子节点 - How to retrieve mulitple child nodes from firebase in android studio Firebase数据库从子android访问节点 - Firebase Database accessing nodes from child android android studio firebase 数据库:如何从子子节点获取数据 - android studio firebase database: how to get data from sub child 如何使用android studio从4层父节点firebase获取子节点数据 - How to get children data from 4 layer of parent nodes firebase using android studio Android-使用键查询节点(Firebase) - Android - Using keys to query nodes (Firebase) 如果使用android studio在firebase中缺少孩子,如何设置错误消息 - How to set error message if a child is missing in firebase using android studio 如何使用Android中的多个where条件从Firebase数据库中获取数据 - How to fetch the data from Firebase database using multiple where condition in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM