错误
FATAL EXCEPTION: main
Process: com.appmaster.akash.messageplus, PID: 978
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.appmaster.akash.messageplus.Results
at com.google.android.gms.internal.zzear.zzb(Unknown Source)
at com.google.android.gms.internal.zzear.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.appmaster.akash.messageplus.TakingQuizDone$1.onChildAdded(TakingQuizDone.java:65)
at com.google.android.gms.internal.zzdri.zza(Unknown Source)
at com.google.android.gms.internal.zzdwu.zzbvb(Unknown Source)
at com.google.android.gms.internal.zzdxa.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:742)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5571)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)
代码
private void fetchResults() {
mDatabaseReference.child("Users").child(id).child("Quiz").child("Results").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Results results = dataSnapshot.getValue(Results.class);
resultsList.add(results);
mAdapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
}
数据库
Results.java
public class Results {
private String Name;
private String Score;
public Results() {
}
public Results(String Name, String Score) {
this.Name = Name;
this.Score = Score;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getScore() {
return Score;
}
public void setScore(String score) {
Score = score;
}
}
错误在数据库中,名称和值都没有修复。喜欢它根据用户决定。
保存数据
String name = RecieversName;
HashMap<String, String> userMap = new HashMap<>();
userMap.put(name, String.valueOf(mScore));
mRef.child("Users").child(RecieversId).child("Quiz").child("Results").setValue(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Intent intent = new Intent(TakingQuiz.this, TakingQuizDone.class);
intent.putExtra("RecieversId",RecieversId);
intent.putExtra("Score", mScore.toString());
startActivity(intent);
finish();
}
}
});
答案 0 :(得分:1)
您希望一次性处理.../Results
下的所有结果。在这种情况下,您应该使用ValueEventListener
而不是ChildEventListener
。
此外,您的Results
类与JSON数据结构不匹配。这意味着当您致电Results
时,Firebase无法自动将JSON的属性读入getValue(Results.class)
对象。相反,你必须自己进行转换。
mDatabaseReference.child("Users").child(id).child("Quiz/Results").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot resultSnapshot: dataSnapshot.getChildren()) {
String user = resultSnapshot.getKey();
String score = resultSnapshot.getValue(String.class);
Results results = new Results(user, score);
resultsList.add(results);
}
mAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
})
答案 1 :(得分:0)
看着你的数据库结构,在一个Results
节点下,似乎(如果我理解的话)你有一个节点,其名称对应于用户的名称和该节点对应的值得分。
关于您的Results.java
类,您的数据库应按如下方式组织数据:
-Quiz
-Question1
-.......
-Results
-Name: "Akash"
-Score: "5"