Firebase数据库路径的子级未通过DataSnapshot附加到循环(Android Studio)

时间:2019-12-08 11:40:49

标签: android firebase firebase-realtime-database

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

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中?

谢谢你热衷:)

1 个答案:

答案 0 :(得分:2)

将引用更改为以下内容:

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

您在参考中缺少节点outfit