简体   繁体   中英

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. I want to display the child elements of my firebase database in a recyclerview. 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:

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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