07-31-2023, 09:31 AM
This is my code for reading Firebase data:
final String[] booknum = {"0"};
databaseReference.child("All BID").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
booknum[0] =Long.toString(snapshot.getChildrenCount());
Toast.makeText(getApplicationContext(), booknum[0],Toast.LENGTH_LONG).show();
}
@Override public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
});
Toast.makeText(getApplicationContext(), booknum[0],Toast.LENGTH_LONG).show();
When I am executing this, the first toast(inside ValueEventListener) prints the right answer (eg '8'). But the toast outside always prints 0 no matter what.
Please Help!
final String[] booknum = {"0"};
databaseReference.child("All BID").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
booknum[0] =Long.toString(snapshot.getChildrenCount());
Toast.makeText(getApplicationContext(), booknum[0],Toast.LENGTH_LONG).show();
}
@Override public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
});
Toast.makeText(getApplicationContext(), booknum[0],Toast.LENGTH_LONG).show();
When I am executing this, the first toast(inside ValueEventListener) prints the right answer (eg '8'). But the toast outside always prints 0 no matter what.
Please Help!