Har bir talaba hemisdagi nomeri bo‘yicha mavzuni oladi. Mavzu bo‘yicha


— Bosqich: Model klassni yaratish



Yüklə 0,87 Mb.
səhifə4/4
tarix15.04.2023
ölçüsü0,87 Mb.
#125331
1   2   3   4
1-testologiya

4 — Bosqich: Model klassni yaratish
Barcha ma’lumotlar va ularning vazifalarini ajratish uchun Model nomli yangi klass yarating. Bu klass View klass borligini bilmaydi.
model.kt
import java.util.*
import kotlin.collections.ArrayListclass Model : Observable() {
// declaring a list of integer
val List: MutableList // constructor to initialize the list
init {
// reserving the space for list elements
List = ArrayList(3) // adding elements into the list
List.add(0)
List.add(0)
List.add(0)
} // defining getter and setter functions
// function to return appropriate count
// value at correct index
@Throws(IndexOutOfBoundsException::class)
fun getValueAtIndex(the_index: Int): Int {
return List[the_index]
} // function to make changes in the activity button's
// count value when user touch it
@Throws(IndexOutOfBoundsException::class)
fun setValueAtIndex(the_index: Int) {
List[the_index] = List[the_index] + 1
setChanged()
notifyObservers()
}
}
5 — Bosqich: MainActivity faylida View va Controller funksiyalarini tashkil qilish
Bu class View va Model o’rtasidagi aloqani o’rnatadi. Model tomonidan taqdim etilgan ma’lumotlar View tomonidan ishlatiladi va tegishli o’zgartirishlar kiritiladi.
MainActivity.kt
import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import java.util.*class MainActivity : AppCompatActivity(), Observer, View.OnClickListener {
// creating object of Model class
var myModel: Model? = null private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root) // creating relationship between the
// observable Model and the
// observer Activity
myModel = Model()
myModel!!.addObserver(this) } // calling setValueAtIndex() method
// by passing appropriate arguments
// for different buttons
override fun onClick(v: View) {
when (v.id) {
R.id.button -> myModel?.setValueAtIndex(0)
R.id.button2 -> myModel?.setValueAtIndex(1)
R.id.button3 -> myModel?.setValueAtIndex(2)
}
} // function to update the view after
// the values are modified by the model
override fun update(arg0: Observable, arg1: Any?) {
// changing text of the buttons
// according to updated values
binding.button.text = "Count: " + myModel!!.getValueAtIndex(0)
binding.button2.text = "Count: " + myModel!!.getValueAtIndex(1)
binding.button3.text = "Count: " + myModel!!.getValueAtIndex(2)
}
}


test javoblari
1 c
2 c
3 d
4 c
5 a
6 c
7 d
8 c
9 b
10 a

Yüklə 0,87 Mb.

Dostları ilə paylaş:
1   2   3   4




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©muhaz.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin