简体   繁体   English

DataSnapshot for 循环不附加 Firebase 数据库路径的子级(Android Studio)

[英]Children of Firebase database path are not attached by DataSnapshot for loop (Android Studio)

i have a problem in my android project which is connected to firebase.我在连接到 firebase 的 android 项目中遇到了问题。 I want to display the child elements of my firebase database in a recyclerview.我想在 recyclerview 中显示我的 firebase 数据库的子元素。 For this i created the following code to save the data in an arraylist.为此,我创建了以下代码以将数据保存在数组列表中。 But there is a problem with attaching the childs of the path - the code does not add the items to the list :|.但是附加路径的子项存在问题 - 代码不会将项目添加到列表中:|。 Is there an error in the reference to the database or an error in the eventlistener?对数据库的引用是否有错误或在事件侦听器中有错误?

private void loadDataOutfitDetail(String userID, String weatherToday){
        // set the firebase path for loading the images of the clothes
        databaseRefOutfit = FirebaseDatabase.getInstance().getReference(userID + "/" + weather1 + "/ID1" + "/details");

        // clear the list for showing only the filtered weather
        uploadDetails.clear();

        // get data out
        databaseRefOutfit.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()){
                    UploadDetails upload = postSnapshot.getValue(UploadDetails.class);
                    // add items to list
                    uploadDetails.add(upload);
                }

                // update the recyclerview
                outfitAdapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(getActivity(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

My Firebase database has the following structure:我的 Firebase 数据库具有以下结构:

USERID
   outfit
      weather1
         ID1
            details
               desciption: Here is a description
               name: MyName
            imageUpperPart
               name: image1
         ID2
            details
               description: Here is another description
               name: NewName
            imageUpperPart
               name: image2

How can I add the values of the details node to the arraylist uploadDetails?如何将 details 节点的值添加到 arraylist uploadDetails?

Thank you keved :)谢谢你 :)

Change the reference to the following:将引用更改为以下内容:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference(userID).child("outfit").child("weather1").child("ID1").child("details");

You are missing the node outfit in your reference.您缺少参考中的节点outfit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM