2

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?

4
  • Where is array list in your code? And what is pA? Commented Feb 1, 2017 at 6:35
  • @MrunalAhirrao pA is the arraylist. Please see the updated question. Commented Feb 1, 2017 at 6:51
  • @cricket_007 please help with this issue Commented Feb 1, 2017 at 7:15
  • 1
    Have you tried moving if (pA != null){pA.clear();} before (out of) the for loop? Commented Feb 1, 2017 at 7:34

1 Answer 1

1

The way a value listener work in firebase, especially the way you are using it, if a child is added to a node you are listening to, you once again receive all the children. This is why you get value1, value1, value2, value1, value2, value3 if you don't clear the arraylist. First value1 is added, then when value2 has added to firebase your listener receives an object with both.

This method is triggered once when the listener is attached and again every time the data, including children, changes. The event callback is passed a snapshot containing all data at that location, including child data. Firebase Documentation

Solution

The easiest way for you to fix this without changing to much code is by clearing pA outside of the for loop. That way you will get all of the currently exisiting 'pA':s each time the event fires.

if (dataSnapshot.getValue() != null) {
        if (pA != null){ 
            pA.clear();
        }
        for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
 .
 .
 .
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.