我正在尝试创建一个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;
}


我怎样才能解决这个问题?

最佳答案

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

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

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中将没有第四项

10-08 15:23