获取 Firebase 的平均评分 Android

Get Average of Rating Firebase Android

我正在关注 如何获得平均评分,但我收到一条错误消息:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.fixitph.client, PID: 25744
com.google.firebase.database.DatabaseException: Failed to convert a value of type java.util.HashMap to double
at com.google.android.gms.internal.zzelw.zzbz(Unknown Source)
at com.google.android.gms.internal.zzelw.zzb(Unknown Source)
at com.google.android.gms.internal.zzelw.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.fixitph.client.ReceiptActivity.onDataChange(ReceiptActivity.java:85)

这是我的代码:

ratingBar.setOnRatingChangeListener(new MaterialRatingBar.OnRatingChangeListener() {
    @Override
    public void onRatingChanged(MaterialRatingBar ratingBar, float rating) {
        final DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference(Common.worker_rating_table)
                .child(Common.workerId).child(uid).child("Rating");

        double dbRating = rating;
        dbRef.setValue(dbRating);
    }
});

public void submitRating(View view) {
    final DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference(Common.worker_rating_table)
            .child(Common.workerId);

    dbRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            double total = 0.0;
            double count = 0.0;
            double average = 0.0;
            for(DataSnapshot ds: dataSnapshot.getChildren()) {
                double rating = dataSnapshot.getValue(Double.class);
                total = total + rating;
                count = count + 1;
                average = total / count;
            }

            final DatabaseReference newRef = dbRef.child(uid);
            newRef.child("averageRating").setValue(average);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            throw databaseError.toException();
        }
    });
}

我的数据库如下所示。

这是引用 ReceiptActivity.java:85

的行
double rating = dataSnapshot.getValue(Double.class);

我想知道这是否是因为我试图找到一个 Double 但评级中有一个 Int,如果是这种情况,我如何调整它以获得评级的平均值?请帮忙。谢谢! :)

for(DataSnapshot ds: dataSnapshot.getChildren()) {
    double rating = Double.parseDouble(ds.child("Rating").getValue());
    total = total + rating;
    count = count + 1;
    average = total / count;
}

您需要从 Rating 子节点获取值并将其解析为 double

    ratingBar.setOnRatingChangeListener(new MaterialRatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChanged(MaterialRatingBar ratingBar, float rating) {
                final DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference(Common.worker_rating_table)
                        .child(Common.workerId).child(Common.user_clients_table).child(uid).child("rating");

                double intRating = rating;

                dbRef.setValue(intRating);
            }
        });

        settingInformation();
    }

public void submitRating(View view) {
    try {
        final DatabaseReference db = FirebaseDatabase.getInstance().getReference();
        final DatabaseReference dbRef = db.child(Common.worker_rating_table).child(Common.workerId).child(Common.user_clients_table);

        dbRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                double total = 0.0;
                double count = 0.0;
                double average = 0.0;

                for(DataSnapshot ds: dataSnapshot.getChildren()) {
                    double rating = Double.parseDouble(ds.child("rating").getValue().toString());
                    total = total + rating;
                    count = count + 1;
                    average = total / count;
                }

                final DatabaseReference newRef = db.child(Common.worker_rating_table).child(Common.workerId).child("AverageRating");
                newRef.child("current").setValue(average);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                throw databaseError.toException();
            }
        });
    } catch (Exception e) {
        Toast.makeText(ReceiptActivity.this, "" + e, Toast.LENGTH_SHORT).show();
    }
}

我必须添加节点来分隔当前的平均值和客户的评级,这样它才能工作。这是数据库的样子: