如何在firebase中获取孩子的孩子名单?

时间:2019-05-29 04:52:31

标签: java android firebase

我正在尝试创建Firebase子数组。这是我的json数据:

{

  "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"
    },

  },
}

现在,我想创建Posts节点的特定uid的postimg子元素的arraylist。
假设CurrentUserIdjOt0sDoVvwcse3yTbpByxnrX2xy2

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

我该如何解决?

1 个答案:

答案 0 :(得分:0)

您的应用程序崩溃是因为如果列表为空,则get()方法没有位置。

您应该检查列表是否具有有效长度,然后填充imageViews,但是您可以拥有1张图像,2张图像,3张或更多张图像,因此您可以考虑一些解决方法,例如创建要填充的imageView列表。

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++;
    }
}

但是再次,请记住,如果将有4张图像,而您将有3张imageViews,则会崩溃,因为imageViewList中将没有第四项