Java – How to iterate DataSnapshot?

How to iterate DataSnapshot?… here is a solution to the problem.

How to iterate DataSnapshot?

I’m trying to traverse the child nodes of a contact node. enter image description here .

But my ListArray contacts are only partially populated. The first two entries fill the array as expected (output from Logcat), but the third entry fails to populate the array. This is MainActivity.java:

public class MainActivity extends AppCompatActivity {
TextView stringTextView;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

stringTextView = (TextView)findViewById(R.id.textView2);

FirebaseDatabase firebaseDatabase= FirebaseDatabase.getInstance().getInstance();
    DatabaseReference dbRef= firebaseDatabase.getReference("County/Dublin");

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

DataSnapshot contactSnapshot = dataSnapshot.child("contacts");
            Iterable<DataSnapshot> contactChildren = contactSnapshot.getChildren();
            ArrayList<Contact> contacts= new ArrayList<>();

for (DataSnapshot contact : contactChildren) {
                Contact c = contact.getValue(Contact.class);
                Log.d("contact:: ", c.name + " " + c.number);
                contacts.add(c);
            }
        }

@Override
        public void onCancelled(DatabaseError databaseError) {}
    });
}
}

and the model class Contact.java:

public class Contact {
public String name;
public String number;

public Contact() {
}

public Contact(String name, String number) {
    this.name = name;
    this.number = number;
}
}

Why is my ListArray not fully populated?

Update

Here’s what I did with the suggested code sample:

@Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            ArrayList<String> names= new ArrayList<>();
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                String name = ds.child("name").getValue(String.class);
                names.add(name);
                String number = ds.child("number").getValue(String.class);
                Log.d("TAG", name);
            }
            for(int i=0; i < names.size(); i++){

stringTextView.setText(stringTextView.getText() + names
                        .get(i) + " , ");
            }
        }

The array is still not populated and is not displayed on the screen.

Solution

Please use this code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("County").child("Dublin").child("contacts");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        ArrayList<String> names= new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String name = ds.child("name").getValue(String.class);
            names.add(name);
            String number = ds.child("number").getValue(String.class);
            Log.d("TAG", name + " / " + number);
        }
        for(String name : names) {
            TextView stringTextView = (TextView) findViewById(R.id.string_text_view);
            stringTextView.setText(stringTextView.getText().toString() + name + " , ");
        }
    }

@Override
    public void onCancelled(DatabaseError databaseError) {}
};
ref.addListenerForSingleValueEvent(eventListener);

Your output will be:

ger / 0213
paddy / 3345556
jeff / 55555

So, with

this data, you can add it to the ArrayList, display it, and do whatever you want with it.

Related Problems and Solutions