0

I'm using a Firebase database to store and read a list of "clubs" in an Android app. Below there's the Json representation of that.

{
  "clubs" : [ {
    "id" : 1,
    "image" : {
      "allocationByteCount" : 589824,
      "byteCount" : 589824,
      "config" : "ARGB_8888",
      "density" : 320,
      "generationId" : 212,
      "height" : 384,
      "mutable" : false,
      "premultiplied" : true,
      "recycled" : false,
      "rowBytes" : 1536,
      "width" : 384
    },
    "name" : "Axis"
  }, {
    "id" : 2,
    "image" : {
      "allocationByteCount" : 589824,
      "byteCount" : 589824,
      "config" : "ARGB_8888",
      "density" : 320,
      "generationId" : 214,
      "height" : 384,
      "mutable" : false,
      "premultiplied" : true,
      "recycled" : false,
      "rowBytes" : 1536,
      "width" : 384
    },
    "name" : "Allies"
  }, {
    "id" : 3,
    "image" : {
      "allocationByteCount" : 589824,
      "byteCount" : 589824,
      "config" : "ARGB_8888",
      "density" : 320,
      "generationId" : 216,
      "height" : 384,
      "mutable" : false,
      "premultiplied" : true,
      "recycled" : false,
      "rowBytes" : 1536,
      "width" : 384
    },
    "name" : "Neutrals"
  } ]
}

I'm using a FirebaseAdapter class to call methods upon the Firebase database. But it returns null when trying to get all clubs.

Bellow theres the method that calls it:

public List<BDClub> getClubs() {

        list.clear();

        database.getReference("clubs").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()){
                    System.out.println(dataSnapshot.getChildrenCount());
                    for(DataSnapshot array : dataSnapshot.getChildren()){
                        clubs = array.getValue(BDClub[].class);
                        for(BDClub c : clubs){
                            list.add(c);
                        }
                    }
                }
            }

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

            }

        });

        return list;
    }

Is there a way to retrieve a list of clubs? Thanks in advance.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

Looking at your Json representation i think clubs is child in main database reference so you can try the following:

FirebaseDatabase.getInstance().getReference().child("clubs").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()){
                    System.out.println(dataSnapshot.getChildrenCount());
                    for(DataSnapshot array : dataSnapshot.getChildren()){
                        clubs = array.getValue(BDClub[].class);
                        for(BDClub c : clubs){
                            list.add(c);
                        }
                    }
                }
            }

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

            }

        }); 

Tip: Do not return list like that from the function because these calls happen asynchronously so there is a huge chance that you will get empty list every time,Use listeners: How to create our own Listener interface in android?

Mayur Gajra
  • 8,285
  • 6
  • 25
  • 41