繁体   English   中英

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

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

我在连接到 firebase 的 android 项目中遇到了问题。 我想在 recyclerview 中显示我的 firebase 数据库的子元素。 为此,我创建了以下代码以将数据保存在数组列表中。 但是附加路径的子项存在问题 - 代码不会将项目添加到列表中:|。 对数据库的引用是否有错误或在事件侦听器中有错误?

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();
            }
        });
    }

我的 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

如何将 details 节点的值添加到 arraylist uploadDetails?

谢谢你 :)

将引用更改为以下内容:

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

您缺少参考中的节点outfit

暂无
暂无

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

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