获取其他子项的值列表

时间:2018-12-08 01:02:13

标签: android firebase firebase-realtime-database

我想获取值以在recyclerview中显示它们,我已经尝试了很多方法但没有运气。

这是我的Firebase数据。

"Users" : {

    "X7aVfZH5oZbIaeyLm9FAjOSr5Gd2" : {
      "images" : {
        "urlImages1" : "https://firebasestorage.googleapis.com/v0/b/anthiefy.appspot.com/o/X7aVfZH5oZbIaeyLm9FAjOSr5Gd2%2FImages%2FImg1.jpg?alt=media&token=e26fd6a2-2be7-4a01-920e-0aed1fe06436",
        "urlImages2" : "https://firebasestorage.googleapis.com/v0/b/anthiefy.appspot.com/o/X7aVfZH5oZbIaeyLm9FAjOSr5Gd2%2FImages%2FImg2.jpg?alt=media&token=40165918-3b46-4b22-b0ef-255965b7855d",
        "urlImages3" : "https://firebasestorage.googleapis.com/v0/b/anthiefy.appspot.com/o/X7aVfZH5oZbIaeyLm9FAjOSr5Gd2%2FImages%2FImg3.jpg?alt=media&token=cfd7d00d-9b1a-44fd-80d4-a2947f7de743",
        "urlImages4" : "https://firebasestorage.googleapis.com/v0/b/anthiefy.appspot.com/o/X7aVfZH5oZbIaeyLm9FAjOSr5Gd2%2FImages%2FImg4.jpg?alt=media&token=995419a7-70f7-4330-b046-d06a4d54453e",
        "urlImages6" : "https://firebasestorage.googleapis.com/v0/b/anthiefy.appspot.com/o/X7aVfZH5oZbIaeyLm9FAjOSr5Gd2%2FImages%2FImg6.jpg?alt=media&token=ee6f566a-a9e5-4e8f-9d53-64d3f3bc97a6",
        "urlImages7" : "https://firebasestorage.googleapis.com/v0/b/anthiefy.appspot.com/o/X7aVfZH5oZbIaeyLm9FAjOSr5Gd2%2FImages%2FImg7.jpg?alt=media&token=883a3321-fbba-49bd-aae3-14b005b4cc5b"
      },
      "locations" : {
        "locations1" : "14.2830817 -89.7263953",
        "locations2" : "14.2661424 -89.7220017",
        "locations3" : "14.2846352 -89.7251991",
        "locations4" : "14.2684987 -89.7266704",
        "locations5" : "14.2684987 -89.7266704",
        "locations6" : "14.2684987 -89.7266704",
        "locations7" : "14.2684987 -89.7266704"
      }

我想从“图像”和“位置”中获取数据。

    imageRef = database.getInstance().getReference("Users").child(userid);


    imageRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {


             for (DataSnapshot mData : dataSnapshot.getChildren()) {


                 String mImages = mData.child("images").getValue(String.class);
                 String mLocations  =  mData.child("locations").getValue(String.class);


                    ItemData itemData = new ItemData();
                    itemData.setImages(mImages);
                    itemData.setLocations(mLocations);

                    data.add(itemData);

            }

            adapter.notifyDataSetChanged();


        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

我想做的是在recyclerview中显示图像和位置。 “图像”键包含url1,url2,url3 ...,与“位置”键相同,我想在适配器中一起显示它们。

请查看链接的图片

1 个答案:

答案 0 :(得分:0)

您使用此代码从images获取数据:

String mImages = mData.child("images").getValue(String.class);

但是,如果我们查看您的JSON,则images属性如下所示:

  "images" : {
    "urlImages1" : "https://firebasestorage.googleapis.com/v0/b/anthiefy.appspot.com/o/X7aVfZH5oZbIaeyLm9FAjOSr5Gd2%2FImages%2FImg1.jpg?alt=media&token=e26fd6a2-2be7-4a01-920e-0aed1fe06436",
    "urlImages2" : "https://firebasestorage.googleapis.com/v0/b/anthiefy.appspot.com/o/X7aVfZH5oZbIaeyLm9FAjOSr5Gd2%2FImages%2FImg2.jpg?alt=media&token=40165918-3b46-4b22-b0ef-255965b7855d",
    "urlImages3" : "https://firebasestorage.googleapis.com/v0/b/anthiefy.appspot.com/o/X7aVfZH5oZbIaeyLm9FAjOSr5Gd2%2FImages%2FImg3.jpg?alt=media&token=cfd7d00d-9b1a-44fd-80d4-a2947f7de743",
    "urlImages4" : "https://firebasestorage.googleapis.com/v0/b/anthiefy.appspot.com/o/X7aVfZH5oZbIaeyLm9FAjOSr5Gd2%2FImages%2FImg4.jpg?alt=media&token=995419a7-70f7-4330-b046-d06a4d54453e",
    "urlImages6" : "https://firebasestorage.googleapis.com/v0/b/anthiefy.appspot.com/o/X7aVfZH5oZbIaeyLm9FAjOSr5Gd2%2FImages%2FImg6.jpg?alt=media&token=ee6f566a-a9e5-4e8f-9d53-64d3f3bc97a6",
    "urlImages7" : "https://firebasestorage.googleapis.com/v0/b/anthiefy.appspot.com/o/X7aVfZH5oZbIaeyLm9FAjOSr5Gd2%2FImages%2FImg7.jpg?alt=media&token=883a3321-fbba-49bd-aae3-14b005b4cc5b"
  },

那是一个完整的JSON对象,而不仅仅是一个字符串。由于值不是字符串,因此您的呼叫将返回null

要从此JSON获取所有图像URL,您将需要遍历其子节点并从那里获取单个值:

for (DataSnapshot imageSnapshot: mData.child("images").getChildren()) {
  System.out.println(imageSnapshot.getValue(String.class));
}

由于可能存在(和存在)多个图像和位置,因此您的ItemData要么 需要处理这些图像,您都需要选择一个单个图像和位置,,您将必须将所有图像/位置转换为单个字符串。