I'm trying to make a quiz app for Android, using Kotlin. I want to be able to send results of every attempt by the player to the Firebase Realtime Database.
I have a following code so far, but it doesn't send anything to the database:
private lateinit var myRef: DatabaseReference
class ResultActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_result)
@Suppress("DEPRECATION")
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
supportActionBar?.hide()
@Suppress("DEPRECATION")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.insetsController?.hide(WindowInsets.Type.statusBars())
} else {
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
)
}
val firebase: FirebaseDatabase = FirebaseDatabase.getInstance()
myRef = firebase.getReference("ArrayData")
val username = intent.getStringExtra(Constants.USER_NAME)
tv_name.text = username
val totalQuestions = intent.getIntExtra(Constants.TOTAL_QUESTIONS, 0)
val correctAnswer = intent.getIntExtra(Constants.CORRECT_ANSWERS, 0)
val result = correctAnswer
tv_score.text = "Your result is $correctAnswer out of $totalQuestions"
btn_finish.setOnClickListener {
val username = intent.getStringExtra(Constants.USER_NAME)
val firebaseInput = DatabaseRow(username, result)
myRef.child("Row 1").setValue(firebaseInput)
startActivity(Intent(this, MainActivity::class.java))
finish()
}
}
}
Does anyone knows what to change so it will send username of the player and his result?
question from:
https://stackoverflow.com/questions/65891775/sending-data-to-firebase-realtime-database-in-kotlin-android-app 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…