0

I want to calculate the average of each dish by their ratings and display in a bar graph, therefore I need the values of each dish with each thier rating

Example 3 people rated milk as {4,5,3} by each user from the user-specific node linkToJSONTree

I need an ArrayList which can store multiple lists inside each index of its own listing:

private void letMeCalculate() 
{

    databaseReference = FirebaseDatabase.getInstance().getReference("/Feedback/"+mdate+"/"+mealtime+"/");

    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            for (DataSnapshot i :  dataSnapshot.getChildren())
            {
                stringArrayList.add(i.getKey());
            }

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
        }
    });

    for (String s : stringArrayList)
    {
        databaseReference = FirebaseDatabase.getInstance().getReference("/Feedback/"+mdate+"/"+mealtime+"/"+s+"/");

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

               for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren())
               {
                   NewDishRating newDishRating = dataSnapshot1.getValue(NewDishRating.class);
                   newDishRatingList.add(newDishRating);

               }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });
    }// end of for loop
}// end of function
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Akshay Bengani
  • 37
  • 2
  • 10
  • This is totally different from the one mentioned as duplicate, Since I have query regarding object containing ArrayList of Objects and the one mentioned only says how to take 1D object array using List<>, mine is quite complicated. – Akshay Bengani Nov 25 '18 at 15:55

2 Answers2

1

When using the stringArrayList outside the callback, it will always be empty due the asynchronous behaviour of the onDataChange() method. If you want to use the stringArrayList, you should use it only inside the callback. So to solve this, please move the second for loop inside the onDataChange() method right after the first for loop ends.

If you need to use that list outside the callback, I recommend you see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thanks since I get to know about genericTypeIndicator and it works good for me – Akshay Bengani Nov 25 '18 at 11:14
  • @AkshayBengani `GenericTypeIndicator` has nothing to do with asynchronous API's. That's the reason Frank van Puffelen marked this question as a duplicate. [GenericTypeIndicator](https://firebase.google.com/docs/reference/android/com/google/firebase/database/GenericTypeIndicator) is useful for resolving types for generic collections at runtime. Please don't confuse future visitors. – Alex Mamo Jan 08 '19 at 08:34
-1

I got the solution instead of using Array list of Array List I can create genericTypeIndicator follow this link for the solution

https://stackoverflow.com/a/41503903/5867698

Akshay Bengani
  • 37
  • 2
  • 10
  • 1
    Using a `GenericTypeIndicator` it will not solve the asynchronous problem. [GenericTypeIndicator](https://firebase.google.com/docs/reference/android/com/google/firebase/database/GenericTypeIndicator) is useful for resolving types for generic collections at runtime, so I don't recommend for this issue this answer for future visitors at all. – Alex Mamo Jan 08 '19 at 08:30