JanusJanus JanusJanus - 6 months ago 43
Android Question

How to iterating Datasnapshot without specifying parent folder ref

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


Well, 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 ; fw88v6xu6wybamg9zzt6
thus 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 this datasnapshot, over each entry and retrieve the information for entry without knowing the parent ref. ( fw88v6xu6wybamg9zzt6 ) ?

all right well, i got to a post similar to my problem;
stackoverflow.com/questions/30669363/firebase-how-get-to-value-of-parent-node-as-string

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 nod, and on the second one i constructed an array to include all the nods.

What i am trying to do is add a specific nod ( e.g. address Value) to an ArrayList i declared outside the onDataChange method, everytime it iterate over a child. so by the end of the for lood, 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 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());

Answer

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());
}
Comments