ipdan4ik

[lb8] RightFragment.kt

Apr 18th, 2021
585
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.lb8
  2.  
  3. import android.content.Context
  4. import android.os.Bundle
  5. import android.view.LayoutInflater
  6. import android.view.View
  7. import android.view.ViewGroup
  8. import android.widget.ArrayAdapter
  9. import android.widget.ListView
  10. import android.widget.TextView
  11. import android.widget.Toast
  12. import androidx.fragment.app.Fragment
  13.  
  14. class RightFragment() : Fragment() {
  15.     lateinit var thisView: View
  16.     private var taskNum: Int = 0
  17.     private var right_answers: Int = 0
  18.     private var count_answers: Int = 0
  19.     private var taskDifficulty: Int = 10
  20.  
  21.     override fun onCreateView(
  22.         inflater: LayoutInflater,
  23.         container: ViewGroup?,
  24.         savedInstanceState: Bundle?): View? {
  25.         return inflater.inflate(R.layout.fragment_right,
  26.             container, false)
  27.     }
  28.  
  29.     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  30.         super.onViewCreated(view, savedInstanceState)
  31.         taskNum = arguments?.getInt("Data", taskNum)!!
  32.         right_answers = arguments?.getInt("right", right_answers)!!
  33.         count_answers = arguments?.getInt("all", count_answers)!!
  34.         taskDifficulty = arguments?.getInt("diff", taskDifficulty)!!
  35.         thisView = view
  36.         val statisticsText: TextView = thisView.findViewById(R.id.statistics)
  37.         statisticsText.text = "Всего ответов: $count_answers, правильных: $right_answers"
  38.         when (taskNum) {
  39.             0 -> createTask("+")
  40.             1 -> createTask("-")
  41.             else -> createTask("*")
  42.         }
  43.     }
  44.  
  45.     override fun onAttach(context: Context) {
  46.         super.onAttach(context)
  47.         mainContext = context
  48.     }
  49.     private lateinit var mainContext: Context
  50.  
  51.     fun createTask(s: String) {
  52.  
  53.         val a = (0..taskDifficulty).random()
  54.         val b = (0..taskDifficulty).random()
  55.         var answer = 0
  56.         when (s) {
  57.             "*" -> answer = a * b
  58.             "+" -> answer = a + b
  59.             "-" -> answer = a - b
  60.         }
  61.  
  62.         val task = "$a $s $b = "
  63.         var answers = MutableList<String>(0) {" "}
  64.  
  65.  
  66.         answers.add(answer.toString())
  67.         for (i in 1..4) {
  68.             answers.add((0..taskDifficulty*taskDifficulty).random().toString())
  69.         }
  70.         answers.shuffle()
  71.         val qTextView: TextView = thisView.findViewById(R.id.task)
  72.         qTextView.text = task
  73.         val adapter = ArrayAdapter<String>(mainContext, R.layout.my_list_item, answers)
  74.         val listView: ListView = thisView.findViewById(R.id.listView)
  75.         listView.adapter = adapter
  76.  
  77.         listView.setOnItemClickListener { parent, view, position, id ->
  78.             val textView = view.findViewById<TextView>(R.id.answer)
  79.  
  80.             var toastText: String
  81.             count_answers += 1
  82.             if (textView.text == answer.toString()) {
  83.                 toastText = "Верно!"
  84.                 right_answers += 1
  85.                 taskDifficulty += 1
  86.             } else {
  87.                 toastText = "Неверно"
  88.                 if (taskDifficulty > 10) {
  89.                     taskDifficulty -= 1
  90.                 }
  91.             }
  92.  
  93.             val toast: Toast = Toast.makeText(mainContext,
  94.                 toastText,
  95.                 Toast.LENGTH_SHORT)
  96.             toast.show()
  97.  
  98.             val statisticsText: TextView = thisView.findViewById(R.id.statistics)
  99.             statisticsText.text = "Всего ответов: $count_answers, правильных: $right_answers"
  100.             (mainContext as OnDataListener).onAnswer(right_answers, count_answers, taskDifficulty)
  101.             createTask(s)
  102.         }
  103.     }
  104. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×