本文介绍了如何迭代Datasnapshot而不指定父文件夹/ ref的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用下面的结构迭代一个Data快照:

pre $ data $ {
images:{
fw88v6xu6wybamg9zzt6:{
0550e909-3b30-4f83-9725-02fbe45b74ce:3,
address:http:// xxxxxxxxxxxxxxx.jpg,
author:username1,
author_id:0550e909-3b30-4f83-9725-02fbe45b74ce,
location:0,
rating:3
},
osgm6v7kfcjwogo5uv21:{
0550e909-3b30-4f83-9725-02fbe45b74ce:4,
address: xxxxxxxxxxxxxxxx.jpg,
author:username1,
author_id:0550e909-3b30-4f83-9725-02fbe45b74ce,
location:0,
rating:4
},
prhpcbrrru7z8x6xtolq:{
0550e909-3b30-4f83-9725-02fbe45b74ce:6,
address: xxxxxxxxxxxxxx.jpg,
author:username2,
author_id:0550e909-3b30-4f83-9725-02fbe45b74ce,
location:0,
等级:6
$ b $ location
location1 $ {
location1:{
author:0550e909-3b30-4f83-9725-02fbe45b74ce,
latitude:11.42222573,
longitude:58.4348011
},
location2:{
author:0550e909-3b30-4f83-9725-02fbe45b74ce ,
latitude:11.42222573,
longitude:38.4333311
}
}
}

特别是我正在做的是获取Data / Images子级的快照,所以它将是自动生成引用的三个条目,如 fw88v6xu6wybamg9zzt6
因此,我不能访问特定的条目来检索作者的地址,所以我让我的应用程序得到

  Iterable< DataSnapshot> imagesDir = snapshot.getChildren(); 

我不确定如何查看此快照的每个条目并检索输入信息知道父母的参考。 ( fw88v6xu6wybamg9zzt6 )?



好的,我得到了

Thankx to克里斯,给我发送正确的道路去寻找它,但仍然不能构建正确的语法,以达到我想要做的。



现在我的方法如下;

$ $ $ $ pre $ public $ onDataChange(DataSnapshot snapshot){
for(DataSnapshot child:snapshot.getChildren )){
Log.i(MyTag,child.getValue()。toString());
imagesDir.add(String.valueOf(child.getValue()));
}
Log.i(MyTag,imagesDir.toString());

从第一个日志我得到每个节点的所有值的字符串,并在第二个我构建了一个包含所有节点的数组。

我要做的是将一个特定节点(例如地址值)添加到我在 onDataChange 方法,每次它遍历一个孩子。所以在for循环结束时,在这种情况下,我将在arraylist中有三个地址(只有值)。

构造for循环为了做到这一点?

那么,我得到的最终代码,并给了我正确的结果如下;


$ b (DataSnapshot child:snapshot.getChildren()){
Log.i(MyTag,$ b pre preublic $ onDataChange(DataSnapshot snapshot) 。child.getValue()的toString());
imagesDir.add(child.child(author)。getValue(String.class));
}
Log.i(MyTag_imagesDirFinal,imagesDir.toString());


解决方案

假设您附加了一个 ValueEventListener images

  public void onDataChange(DataSnapshot imagesSnapshot){
for(DataSnapshot imageSnapshot:imagesSnapshot.getChildren()){
imagesDir.add(imageSnapshot.child(address)。getValue(String.class));
}
Log.i(MyTag,imagesDir.toString());
}


I would like to iterate through a Data snapshot with the following structure;

 {
"data" : {
"images" : {
  "fw88v6xu6wybamg9zzt6" : {
    "0550e909-3b30-4f83-9725-02fbe45b74ce" : 3,
    "address" : "http://xxxxxxxxxxxxxxx.jpg",
    "author" : "username1",
    "author_id" : "0550e909-3b30-4f83-9725-02fbe45b74ce",
    "location" : "0",
    "rating" : 3
  },
  "osgm6v7kfcjwogo5uv21" : {
    "0550e909-3b30-4f83-9725-02fbe45b74ce" : 4,
    "address" : "xxxxxxxxxxxxxxxx.jpg",
    "author" : "username1",
    "author_id" : "0550e909-3b30-4f83-9725-02fbe45b74ce",
    "location" : "0",
    "rating" : 4
  },
  "prhpcbrrru7z8x6xtolq" : {
    "0550e909-3b30-4f83-9725-02fbe45b74ce" : 6,
    "address" : "xxxxxxxxxxxxxx.jpg",
    "author" : "username2",
    "author_id" : "0550e909-3b30-4f83-9725-02fbe45b74ce",
    "location" : 0,
    "rating" : 6
  }
},
"locations" : {
  "location1" : {
    "author" : "0550e909-3b30-4f83-9725-02fbe45b74ce",
    "latitude" : 11.42222573,
    "longitude" : 58.4348011
  },
  "location2" : {
    "author" : "0550e909-3b30-4f83-9725-02fbe45b74ce",
    "latitude" : 11.42222573,
    "longitude" : 38.4333311
  }
 }
}

Specifically what I am doing is getting a snapshot of Data/Images children, so it would be the three entries with the auto generated references such as fw88v6xu6wybamg9zzt6thus I can not accesses a specific entry to retrieve the address for example of the author, so I made my app get

    Iterable<DataSnapshot> imagesDir = snapshot.getChildren();

and I am not sure how to go over each entry of this snapshot and retrieve the information for entry without knowing the parent ref. ( fw88v6xu6wybamg9zzt6 ) ?

All right well, I got to a post

Thankx to Chris, for sending me on the right path to search for it, but still not able to construct the right syntax to reach what I am trying to do.

For now my method is as follow;

  public void onDataChange(DataSnapshot snapshot) {
            for (DataSnapshot child: snapshot.getChildren()) {
                Log.i("MyTag", child.getValue().toString());
                imagesDir.add(String.valueOf(child.getValue()));
            }
            Log.i("MyTag", imagesDir.toString());

from the first Log I get a string of all the values for each node, and on the second one I constructed an array to include all the nodes.

What i am trying to do is add a specific node ( e.g. address Value) to an ArrayList I declared outside the onDataChange method, everytime it iterates over a child. so by the end of the for loop, I would have three addresses in the arraylist (only the values) in this case.

What is the proper way to construct a for loop in order to do that?

Well, the final code I got to and gave me the right outcome is as following;

              public void onDataChange(DataSnapshot snapshot) {
            for (DataSnapshot child: snapshot.getChildren()) {
                Log.i("MyTag", child.getValue().toString());
                imagesDir.add(child.child("author").getValue(String.class));
            }
            Log.i("MyTag_imagesDirFinal", imagesDir.toString());
解决方案

Assuming that you attach a ValueEventListener to images:

public void onDataChange(DataSnapshot imagesSnapshot) {
    for (DataSnapshot imageSnapshot: imagesSnapshot.getChildren()) {
        imagesDir.add(imageSnapshot.child("address").getValue(String.class));
    }
    Log.i("MyTag", imagesDir.toString());
}

这篇关于如何迭代Datasnapshot而不指定父文件夹/ ref的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 16:11