gpt4 book ai didi

android - 获取 Firebase Android 评分的平均值

转载 作者:行者123 更新时间:2023-11-30 00:02:50 26 4
gpt4 key购买 nike

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

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$2.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

关于android - 获取 Firebase Android 评分的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49580573/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com