嘿,我正在嘗試計算用戶正確回答問題的次數。這是我的數據庫是如何存儲計算變量的數量等於firebase中的值
userid-> questionsid1-> correectlyanswer 「1」
userid-> questionsid2-> correectlyanswer 「0」
userid-> questionsid3-> correectlyanswer 「0」
userid-> questionsid4-> correectlyanswer 「1」
1表示該答案是正確的,0是不正確。
這裏是代碼。 questioncode的值是正確的,但checkCorrectAnswer字符串中的值始終爲空。
databaseReference1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int questionsolved = 0;
for (DataSnapshot userAnswerSnapshot : dataSnapshot.getChildren()) {
String questionCodeKey = userAnswerSnapshot.getKey();
String checkCorrectAnswer = userAnswerSnapshot.child(questionCodeKey).child("checkAnswerCorrect").getValue(String.class);
if (checkCorrectAnswer.equals("1")) {
questionsolved = questionsolved + 1;
}
}
questionSolvedTextview.setText(questionsolved + "");
};
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
請編輯您的問題,以顯示準確的JSON(文字,沒有截圖),因爲它是存儲在數據庫中。您可以通過點擊[Firebase數據庫控制檯](https://console.firebase.google.com/project/_/database/data)中的「導出JSON」鏈接來獲取此信息。 –
是的,請執行@FrankvanPuffelen所要求的編輯。你也可以提供給我們你的databaseReference1變量是指什麼? –
你可以發佈json數據來了解你的問題嗎? –