1

I have some data under same reference in the database and I'm trying to store it in a ArrayList<String>.

Here's how:

    mDatabase.child("child").child(uid).addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            if (dataSnapshot.getValue() != null){
               ids.add(dataSnapshot.getValue().toString());
               Log.d("ids", ids.toString());
            } else {
                Toast.makeText(getBaseContext(), "No data!", Toast.LENGTH_SHORT).show();
            }

        }
        ...
        ...
    });

The reference mDatabase.child("child").child(uid)... has 2 keys stored in it and they are getting stored in ids one-by-one like this:

D/ids: [-Kayr_ENTy4QZQKn3Bgx]
D/ids: [-Kayr_ENTy4QZQKn3Bgx, -KaywN_5pTOrB0ooBIC3]

What I want to know is how to make them get added at once in ids and not one-by-one?

UPDATE 1:-

the data structure is kind of like this:

-app
  -child
    -uid
      -1484967071: -Kayr_ENTy4QZQKn3Bgx
      -1484967222: -KaywN_5pTOrB0ooBIC3

and using valueEventListener(), as suggested by cartant, is resulting in this getting retrieved:

D/ids: [{1484967222=-KaywN_5pTOrB0ooBIC3, 1484967071=-Kayr_ENTy4QZQKn3Bgx}]

though I only want the keys, which are -KaywN_5pTOrB0ooBIC3 and -Kayr_ENTy4QZQKn3Bgx.

Community
  • 1
  • 1
Hammad Nasir
  • 2,889
  • 7
  • 52
  • 133
  • 1
    You say you want the keys, but in your example, you're pointing out the values. In your data structure above, it looks like the keys are 1484967071 and 1484967222, and the values are "-Kayr_ENTy4QZQKn3Bgx" and "-KaywN_5pTOrB0ooBIC3". Do you really want the keys or the values? – Doug Stevenson Jan 22 '17 at 05:27
  • @DougStevenson by `keys` I meant the string `-KaywN_5pTOrB0ooBIC3` and this `-Kayr_ENTy4QZQKn3Bgx` not the key from the `key:value` pair. Sorry, for the confusion. – Hammad Nasir Jan 22 '17 at 06:42
  • I think the given answer should work for you. Make sure you're getting the value of the iterated child snapshot, not the snapshot passed to your onDataChange(). – Doug Stevenson Jan 22 '17 at 08:01

1 Answers1

4

If you want the entire snapshot to be delivered to the listener, use either addValueEventListener or addListenerForSingleValueEvent.

If you use addValueEventListener, the listener will be called with the initial snapshot and again if the database changes.

And if you use addListenerForSingleValueEvent, the listener will be called only once with the initial snapshot.

The snapshot received by the listener will include all of the children. To iterate them you would do something like this:

@Override
public void onDataChange(DataSnapshot snapshot) {
    ArrayList<String> ids = new ArrayList<String>();
    for (DataSnapshot childSnapshot: snapshot.getChildren()) {
        ids.add(childSnapshot.getValue().toString());
    }
}
cartant
  • 57,105
  • 17
  • 163
  • 197
  • thanks cartant ...though it has't solved my problem, but it has given me some hints! Need yours and @FrankvanPuffelen 's help here too: http://stackoverflow.com/q/41710448/6144372 Please :) – Hammad Nasir Jan 21 '17 at 21:02