0

I want to count elements in a firebase database, I have seen different topics and tried this code:

final Query dataQuery = myRef.equalTo(MainActivity.user.getUid()).getRef();
    dataQuery.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Log.e("ERROR",""+dataSnapshot.child(MainActivity.user.getUid()).getChildrenCount());

        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.e(TAG, "onCancelled", databaseError.toException());
        }
    });

Now, the value in log error is correct but, if I try to assign it in field, or static field is always 0 out of this method; How can I use this value in other class?

Simone
  • 1
  • 2
  • That's because Firebase loads data asynchronously. Where else do you want to use this value? – Rosário Pereira Fernandes Jan 15 '18 at 15:59
  • I want use it in my main class, for create a toDo list with exactly database element numeber. – Simone Jan 15 '18 at 16:01
  • I can tell you're a beginner in Android. I recommend checking out [this tutorial](https://inducesmile.com/android/a-simple-android-todo-list-app-with-recyclerview-and-firebase-real-time-database/) to build your app. Or take the [Free Android Course on Udacity](https://www.udacity.com/course/new-android-fundamentals--ud851). This will help you learn the best practices and make your app better. The way you have designed it right now makes it hard for us to help. – Rosário Pereira Fernandes Jan 15 '18 at 17:46
  • Did that answer help? – dazza5000 Jan 28 '18 at 18:40

2 Answers2

1

You need to use a callback and call a method on the callback in your onDataChange. Then once that callback is returned you can continue with the rest of your logic.

You can see an example of that here:

https://github.com/Austin-Android/austin-feeds-me/blob/master/app/src/main/java/com/austindroids/austinfeedsme/data/firebase/FirebaseEventsDataSource.java#L40

fireBase.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                Event event = snapshot.getValue(Event.class);
                events.add(event);
            }
            callback.onEventsLoaded(events);
        }

        @Override
        public void onCancelled(DatabaseError firebaseError) {

        }
    });
dazza5000
  • 7,075
  • 9
  • 44
  • 89
1

You cannot simply take that value and use it outside onDataChange() method, because it will always be null. This is happening because this method has an asynchronous behaviour, which means that is called even before you are getting the data out from the database. A quick fix would be to use that value only inside onDataChange() method, or to dive into the asynchronous world and see the last part of my answer from this post.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193