简体   繁体   中英

How to get the list of child of child in firebase?

I am trying to make an arraylist of firebase child. Here is my json data:

{

  "Posts" : {

    "4dBtdsgxgMdWahQKLW3dfZ3QYnv110-05-201909:19:00 PM" : {

      "location" : "",
      "postimg" : {
            "0":"http://something.com"
            "1":"http://something.com"
            "2":"http://something.com"
            },
      "uid" : "4dBtdsgxgMdWahQKLW3dfZ3QYnv1"
    },
    "4dBtdsgxgMdWahQKLW3dfZ3QYnv110-05-201909:28:48 PM" : {
      "location" : "",
      "postimg" : {
            "0":"http://something.com"
            "1":"http://something.com"
            },
      "uid" : "4dBtdsgxgMdWahQKLW3dfZ3QYnv1"
    },

    "jOt0sDoVvwcse3yTbpByxnrX2xy224-05-201912:29:14 AM" : {

      "location" : "",
      "postimg" : {
            "0":"http://something.com"
            "1":"http://something.com"
            "2":"http://something.com"
            "3":"http://something.com"
            },
      "uid" : "jOt0sDoVvwcse3yTbpByxnrX2xy2"
    },
    "jOt0sDoVvwcse3yTbpByxnrX2xy228-05-201902:15:20 AM" : {

      "location" : "",
     "postimg" : {
            "0":"http://something.com"
            "1":"http://something.com"
            },
      "uid" : "jOt0sDoVvwcse3yTbpByxnrX2xy2"
    },
    "jOt0sDoVvwcse3yTbpByxnrX2xy229-05-201912:27:16 AM" : {

      "location" : "",
     "postimg" : {
            "0":"http://something.com"
            "1":"http://something.com"
            },
      "uid" : "jOt0sDoVvwcse3yTbpByxnrX2xy2"
    },

  },
}

Now i want to make arraylist of postimg child of specific uid of Posts node.
Suppose CurrentUserId is jOt0sDoVvwcse3yTbpByxnrX2xy2 .

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    //Returning the layout file after inflating
    //Change R.layout.tab1 in you classes
    View view  =  inflater.inflate(R.layout.photos_user_fragment_tab, null);

    mAuth=FirebaseAuth.getInstance();
    CurrentUserId=mAuth.getCurrentUser().getUid();
    UserRef=FirebaseDatabase.getInstance().getReference().child("Users").child(CurrentUserId);

    PostRef=FirebaseDatabase.getInstance().getReference().child("Posts");
    final List<ImageView> imageViewList = new ArrayList<>();
    imageViewList.add(firstPhoto);
    imageViewList.add(secondPhoto);
    imageViewList.add(thirdPhoto);

    PostRef.orderByChild("uid").equalTo(CurrentUserId).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot)
        {

            ArrayList<String> list = new ArrayList<>();
                for (DataSnapshot ing : dataSnapshot.child("postimg").getChildren())
                {
                    list.add(ing.getValue().toString());
                }



            int index = 0;
            for(ImageView imageView : imageViewList) {
                if (index == list.size()) break;
                Picasso.get().load(list.get(index)).into(imageView);
                index++;
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });




    return view;
}

How can I fix this?

Your app is crashing because if list is empty then there is no position for get() method.

You should either check if list has valid length and then fill the imageViews, but then you can have 1 image, 2 images, 3 or more, so you could think for some workaround, eg create list of imageViews to fill into.

List<ImageView> imageViewList = new ArrayList<>();
imageViewList.add(firstPhoto);
imageViewList.add(secondPhoto);
imageViewList.add(thirdPhoto);

for (DataSnapshot childDataSnapshot: dataSnapshot.getChildren()) {
    ArrayList <String> list = new ArrayList<>();
    for (DataSnapshot ing: childDataSnapshot.child("postimg").getChildren()) {
        list.add(ing.getValue().toString());
    }

    int index = 0;
    for(ImageView imageView : imageViewList) {
        if(index == list.size()) break;
        Picasso.get().load(list.get(index)).into(imageView);
        index++;
    }
}

But again, remember that if there will be 4 images and you will have 3 imageViews there will be crash as there won't be fourth item in the imageViewList

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