0

My log can print all Exercise object. But listExercise is null when I call. What's the problem?

ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                Exercise exercise = postSnapshot.getValue(Exercise.class);
                String temp = exercise.toString();
                Log.d("exercise: ", temp + "\n"); // can log all
                listExercises.add(exercise);
            }
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {
            Log.d("The read failed: ", firebaseError.getMessage());
        }
    });
Log.d("LISTSIZE: ", String.valueOf(listExercises.size())); // return 0

[UPDATE] The answer for anyone need:

@Override
        public void onDataChange(DataSnapshot snapshot) {
            for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                Exercise exercise = postSnapshot.getValue(Exercise.class);
                listExercises.add(exercise);
            }
            // action with data here
            Exercise ex = listExercises.get(0);
            tvQuestion.setText(ex.getQuestion);
        }
10
  • Are you getting some error stack trace? Commented Apr 18, 2016 at 8:51
  • No, It just return an empty list when I logged: Log.d("LISTSIZE: ", String.valueOf(listExercises.size())); Commented Apr 18, 2016 at 8:53
  • Probably you are checking the list before Exercise has been inserted. Notice this works asynchronously. Put the Log of "LISTSIZE" after listExercises.add(exercise); Commented Apr 18, 2016 at 9:02
  • Of course, I put Log of "LISTSIZE" after listExercises.add(exercise); I will edit my question for more detail. Commented Apr 18, 2016 at 9:06
  • Is your listExcercise a custom implementation of List or is class Exercise doing something in the method toString(); Maybe if you put the rest of the code I can help you better :) Commented Apr 18, 2016 at 9:09

3 Answers 3

3

When the method "paintListInScreen" is called, you know for sure the list have some content (or maybe not, that depends on the server, but you are being consistent with threads).

ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        for (DataSnapshot postSnapshot : snapshot.getChildren()) {
            Exercise exercise = postSnapshot.getValue(Exercise.class);
            String temp = exercise.toString();
            Log.d("exercise: ", temp + "\n"); // can log all
            listExercises.add(exercise);
        }
         Log.d("LISTSIZE: ", String.valueOf(listExercises.size())); // return 0
        paintListInScreen(listExercises);
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {
        Log.d("The read failed: ", firebaseError.getMessage());
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks I will try it
You are correct, jDur. After I tried, I found it two point for we can use data I get: The first, it's time after onDataChange method. The second, when you do some action after sync data, maybe it's onClick of button...etc..
Your welcome :). If you think this is a correct answer please mark it as correct so other users could find it helpful.
0

For Java 8 and newer versons (For Android API Level 24 is required ):

int size = dataSnapshot.getChildren().spliterator().getExactSizeIfKnown() ;

For Older Versions:

int size = 0 
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
    size++ ;
}

1 Comment

the top one also requires API 24
0

To get the size of datasnapshot object:

dataSnapshot.getChildrenCount();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.