java - 如何获取firebase中child的child列表?

标签 java android firebase

我正在尝试创建 firebase child 的数组列表。这是我的 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"
    },

  },
}

现在我想创建 Postimg 子节点的数组列表,该子节点是 Posts 节点的特定 uid。
假设 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 个或更多图像,因此您可以考虑一些解决方法,例如创建要填充的 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 个 imageView,则会发生崩溃,因为 imageViewList 中不会有第四项

关于java - 如何获取firebase中child的child列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56353314/

相关文章:

机器人 : moving Image one point to another point

java - 我想使用 HttpClient4 对并发发送请求进行 RESTful API 压力测试,我可以使用一个 HttpClient 实例吗?

java - JSF 在绑定(bind)时抛出参数类型不匹配

java - 无法将 TextView 的值设置为 Activity 或 fragment 类中非静态方法的结果

firebase - "firebase serve --only functions"不起作用

node.js - firebase 函数中的 "firebase serve"未运行最新更改

javascript - 使用 mocha 进行测试时导入 firebase-functions-test 时出错

java - 警告 : No file found for:/sign

java - 在 Android 中通过电子邮件发送 map 图片

android - 从 webview 启动 whatsapp