Advertisement
Roxy_45

how to go the next page

Feb 27th, 2024
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.38 KB | None | 0 0
  1. val submitButton = findViewById<Button>(R.id.submit_button)
  2.         submitButton.setOnClickListener {
  3.             showAlertDialog()
  4.         }
  5.  
  6. private fun showAlertDialog() {
  7.         AlertDialog.Builder(this)
  8.             .setTitle("Submit Confirmation")
  9.             .setMessage("Are you sure you want to submit?")
  10.             .setPositiveButton("Yes") { dialog, which ->
  11.                 dialog.dismiss()
  12.                 Toast.makeText(this, "Submission Confirmed", Toast.LENGTH_SHORT).show()
  13.                 calcres()
  14.  
  15.             }
  16.             .setNegativeButton("No") { dialog, which ->
  17.                 dialog.dismiss()
  18.             }
  19.             .show()
  20.     }
  21.  
  22.     private fun calcres() {
  23.         val radioGroupIds = arrayOf(
  24.             R.id.RG1, R.id.RG2, R.id.RG3, R.id.RG4, R.id.RG5,
  25.             R.id.RG6, R.id.RG7, R.id.RG8, R.id.RG9, R.id.RG10,
  26.             R.id.RG11, R.id.RG12 // Add IDs for the new RadioGroups
  27.         )
  28.  
  29.         val correctans = mutableListOf(
  30.             "True", "False", "True", "False", "True",
  31.             "False", "True", "False", "True", "False",
  32.             "True", "False" // Add the correct answers for the new questions
  33.         )
  34.         val urans = mutableListOf<String>()
  35.         var score = 0
  36.  
  37.         radioGroupIds.forEachIndexed { index, radioGroupId ->
  38.             val radioGroup = findViewById<RadioGroup>(radioGroupId)
  39.             val selectedRadioButtonId = radioGroup.checkedRadioButtonId
  40.             if (selectedRadioButtonId != -1) {
  41.                 val radioButton = findViewById<RadioButton>(selectedRadioButtonId)
  42.                 val selectedText = radioButton.text.toString()
  43.                 urans.add(selectedText)
  44.                 if (selectedText == correctans[index]) {
  45.                     score++
  46.                 }
  47.             } else {
  48.                 urans.add("Not Answered")
  49.             }
  50.         }
  51.  
  52.         tempScore = score
  53.         checkPermissionsAndShowNotification(score)
  54.         val intent = Intent(this, MainActivity2::class.java).apply {
  55.             putExtra("score", score)
  56.             putStringArrayListExtra("ans", ArrayList(urans))
  57.         }
  58.         startActivity(intent)
  59.     }
  60.  
  61. #ON NEXT PAGE
  62. val getans:ArrayList<String>?=intent.getStringArrayListExtra("ans")
  63.         val answersText = getans?.joinToString(separator = "\n") ?: "No answers provided"
  64.         val score=intent.getIntExtra("score",0)
Tags: #kotlin
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement