In my code i made
ArrayList<String> allpromo =new ArrayList<>();
as global variable and in onCreate(){}
i wrote
database.getReference().child("PromoCodes").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
allpromo.clear();
for(DataSnapshot snapshot1 : snapshot.getChildren())
{
String val=snapshot1.child("Promo").getValue(String.class);
Toast.makeText(getApplicationContext(), val, Toast.LENGTH_SHORT).show();
allpromo.add(val);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
if(allpromo.isEmpty()) {
Toast.makeText(getApplicationContext(), "0", Toast.LENGTH_SHORT).show();
}
It is displaying me all contents of "Promo" from firebase but why it is not able to add items in arraylist. Beacause it Toast "0" .It means mine arraylist is remain empty.And still it Toast items of firebase
But why?enter image description here
and my database is