I'm retrieving some data from a particular FirebaseDatabase reference and trying to add it in an ArrayList<String> but something strange is happening as they aren't getting added one after another.
somewhere in onCreate():
pA = new ArrayList<>();
Here's my code:
aRef.child(reID).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue() != null) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
Map<String, String> map = (Map<String, String>) childSnapshot.getValue();
if (pA != null){
pA.clear();
}
pA.add(map.get("pA"));
Log.d("pA", String.valueOf(pA));
}
} else {
Toast.makeText(getBaseContext(), "no data available yet", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Here's data structure:
-app
-child
-reID
-uniqueID1
-userID1
-key: value
-key: value
-pA: value1
-uniqueID2
-userID2
-key: value
-key: value
-pA: value2
-uniqueID3
-userID3
-key: value
-key: value
-pA: value3
Here's what getting logged out:
D/pA: [value1]
D/pA: [value2]
D/pA: [value3]
I tried removing if (pA != null) {pA.clear();}, but then I got this log:
D/pA: [value1, value1, value2, value1, value2, value3]
What I want is this:
D/pA: [value1, value2, value3]
Please let me know what's going wrong here and how can I achieve above given result?
pAis the arraylist. Please see the updated question.if (pA != null){pA.clear();}before (out of) theforloop?