I have a Firebase Realtime database and I would like to retreive some data and put them in an arraylist.
ArrayList<Item> itemsArray;
List<Item> itemsList;
DatabaseReference shopRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shop);
itemsArray = new ArrayList<>();
shopRef = FirebaseDatabase.getInstance().getReference();
Query query = shopRef.child("shop");
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Item item = new Item();
item.setLogo(snapshot.child("Logo").getValue().toString());
item.setName(snapshot.child("Name").getValue().toString());
item.setDescription(snapshot.child("Description").getValue().toString());
itemsArray.add(item); //itemsArray is full
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
itemsList = itemsArray; // itemsArray is null
}
The problem is that after the listener, it looks simple to fix but I don’t understand why the arraylist is null.
The database works properly
The Item class works properly