0

I am having trouble adding values from my Firebase Realtime Database to a List. The values are being retrieved properly, and can be printed, however- I am unable to add these values to a list. What am I doing wrong?

@Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot collectionSnapshot : dataSnapshot.getChildren()) {
                    collectionName = collectionSnapshot.child("name").getValue(String.class);
                    names.add(collectionName);
                    count = collectionSnapshot.child("progress").getValue(int.class);
                    numbers.add(count);
                    Toast.makeText(CollectionGraph.this, collectionName + count, Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

I'm attempting to use the values within the two arrays on a pie chart, however, upon running the app, I am met with an empty pie chart Empty output Firebase Realtime Database

  • Can you edit the post to show the sample output and possibly how your real-time DB looks like as well, pls? – pmjr Jun 28 '22 at 17:05
  • 3
    Not enough info to say for sure, but it sounds like [this issue](https://stackoverflow.com/a/70178210/9473786) - are you making the pie chart immediately after declaring that callback? You should create the chart inside `onDataChange` or the chart will be created before the firebase call finishes – Tyler V Jun 28 '22 at 17:30
  • Agree with Tyle's comment. There is no way you can simply do that. Firebase API is asynchronous. So please check the duplicate to see how can you solve this using a callback. You might also be interested in reading this [resource](https://medium.com/firebase-tips-tricks/how-to-read-data-from-firebase-realtime-database-using-get-269ef3e179c5). – Alex Mamo Jul 06 '22 at 08:30

0 Answers0