Android Java 从 Firebase 获取每家餐厅的平均评分

Android Java get the average rating for each restaurant from Firebase

我已经尝试了数千次来获取每家餐厅的价值,然后从每家餐厅的评分中获取平均值。 这是我的 firebase,其中有 3 个用户对餐厅进行了评分: Firebase database 这是我的 Java 代码:

private void displayRating(){ mRatingBar.setVisibility(View.VISIBLE); mRatingBar.setOnRatingBarChangeListener(新RatingBar.OnRatingBarChangeListener() { @覆盖 public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { final DatabaseReference mRatingDb = FirebaseDatabase.getInstance().getReference().child(namesList.get(pos)).child("rating").child(currentUserId); mRatingDb.setValue(评级); final DatabaseReference averageRatingDb = FirebaseDatabase.getInstance().getReference().child(namesList.get(pos)).child("Average");

            mRatingDb.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    for (DataSnapshot ds : dataSnapshot.getChildren()) {
                        double total = 0.0;
                        double count = 0.0;
                        double rating = ds.child(currentUserId).getValue(double.class);
                        total = total + rating;
                        count = count + 1;
                        double average = total / count;
                        averageRatingDb.setValue(average);
                    }
                }
                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });

        }

    });
}

出于某种原因,我觉得 onDataChange() 上的 for 循环根本不起作用。我无法获得任何评分,因此我可以计算平均评分。

我如何从用户那里获取价值,然后分别获取每家餐厅的平均值?

如果有人可以帮忙,我很迷茫。 谢谢。

你可以这样试试:

final DatabaseReference mRatingDb = FirebaseDatabase.getInstance().getReference().child(namesList.get(pos)).child("rating").child(currentUserId);
final DatabaseReference mQueryDb = FirebaseDatabase.getInstance().getReference().child(namesList.get(pos)).child("rating")
final DatabaseReference averageRatingDb = FirebaseDatabase.getInstance().getReference().child(namesList.get(pos)).child("Average");
double total = 0.0;
double count = 0.0;
mRatingDb.setValue(rating).addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
           mQueryDb.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    double rating = ds.getValue(double.class);
                    total = total + rating;
                    count = count + 1;
                    double average = total / count;
                    averageRatingDb.setValue(average);
                }
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

                }
              });

            }

          }); 
        }
    });