how to retrieve many values under id

62 views Asked by At

I am retrieving many values under id, but it's not working. It should be very easy, but i am stuck.

This is my data structure:

Attendance
    x73aXTQHXHXHQ 
        current
          value: "Bill Gates"
        company
          value: "Apple"
        history
            -La7wew7hdxccdcdzssf
                   value: 156000000001
            -La7wptthd3ceeqoz34f
                   value: 156000000002
            -La7wew7hdxccdcdzssf
                   value: 156000000003

I've tried this code and succeed only retrieved children count Output: 3, but not children values 156000000001 like I want. This is my code, any help thank you.

if (uid != null) {
   ref.child(uidKeys).child(uid).child("history").addValueEventListener(new ValueEventListener() {
     @Override
     public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

          int countUnderHistory = (int) dataSnapshot.getChildrenCount();
          long[] dataHistory = new long[countUnderHistory];
          int index = 0;
          for (DataSnapshot push : dataSnapshot.getChildren()) {
               NonDuplicateAttendance nDa = push.getValue(NonDuplicateAttendance.class);

                     if (nDa != null) {
                         dataHistory[index] = nDa.getuHistory();
                     }
                     index++;
          }
      }

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

NonDuplicateAttendance class:

package com.example.aspireone.myapplication;

public class NonDuplicateAttendance {

    private long value;

    public NonDuplicateAttendance(long value) {
        this.value = value;
    }

    public long getuHistory() {
        return value;
    }
}
1

There are 1 answers

3
Frank van Puffelen On BEST ANSWER

Since you didn't share your NonDuplicateAttendance I can't say what's going wrong there. But to print the values in the history:

ref.child(uidKeys).child(uid).child("history").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
            System.out.println(snapshot.child("value").getValue(Long.class));
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        throw databaseError.toException(); // don't ignore errors
    }
});

Edit

Your class doesn't work, because it doesn't declare a public value property. To fix this:

public class NonDuplicateAttendance {

    public  long value;

    public NonDuplicateAttendance() { }

    public NonDuplicateAttendance(long value) {
        this.value = value;
    }

    @Exclude
    public long getuHistory() {
        return value;
    }
}

The changes:

  • The value field is public, so that the SDK recognizes it as matching the value property in your JSON.
  • I added a no-argument constructor, because otherwise the SDK won't be able to instantiate your class.
  • I annotated your getuHistory() method to be excluded from the database, since otherwise the SDK will write a uHistory property too.