簡體   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