关于我如何获得评分的平均值,我正在关注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,如果是这样的话,我怎么能调整这个来获得收视率的平均值?请帮忙。谢谢! :)
答案 0 :(得分:1)
for(DataSnapshot ds: dataSnapshot.getChildren()) {
double rating = Double.parseDouble(ds.child("Rating").getValue());
total = total + rating;
count = count + 1;
average = total / count;
}
您需要从Rating
子项获取值并将其解析为double
。
答案 1 :(得分:0)
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();
}
}
我必须添加节点来分隔当前平均值和客户端的评级,以便它可以工作。这就是数据库的样子: