I'm trying to iterate over the children of the contacts node. .
However my ListArray contacts
only partially populates. The first two entries populate the array as expected (output from Logcat) but the 3rd entry fails to populate the array. Here is the 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 completely populating?
UPDATE
Here is what I've done with the suggested code example:
@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) + " , ");
}
}
Array still not populating and no display to the screen.
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, having this data, you can add it to an ArrayList, display it, do what you want with it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With