0

I want to retrieve a list of data in Firebase and store it locally in my ArrayList. I want to get the number of count inside the ArrayList.

mFirebaseDatabase.getReference("attendance").child(MainActivity.userName).child(subCode).child("student_absences").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot child : dataSnapshot.getChildren()) {
            StudentAttendance attendanceInit = child.getValue(StudentAttendance.class);
            ss.add(attendanceInit);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});
// here i want to get the size by using ss.size() but my above code, the size is always zero
koceeng
  • 2,169
  • 3
  • 16
  • 37
  • it means you are not able to fetch data from firebase – Vivek Mishra Mar 20 '17 at 12:57
  • i can fetch data if ss.size is inside onDataChange() (plss see the edited code) – mycohbasilan Mar 20 '17 at 13:01
  • 2
    You have to get the size inside the onDataChanged method. If you want to do it where the comment is placed, off course size is going to be zero as data is not retrieved at this point. – Nicolás Arias Mar 20 '17 at 13:13
  • As Nicolas says: you'll need to put the code that needs the snapshot *inside* `onDataChange()`. For a longer explanation, see my answer here: http://stackoverflow.com/questions/33203379/setting-singleton-property-value-in-firebase-listener – Frank van Puffelen Mar 20 '17 at 14:08
  • the problem sir is, i just need to access the Arraylist "ss" after retrieving the data – mycohbasilan Mar 21 '17 at 04:49

0 Answers0