Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

Sending data to firebase realtime database in Kotlin Android app

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...