SHARE
TWEET

Untitled

a guest Apr 20th, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package sample
  2.  
  3. import android.arch.lifecycle.MutableLiveData
  4. import android.arch.lifecycle.Observer
  5. import android.os.Bundle
  6. import android.support.v7.app.AppCompatActivity
  7. import android.view.View
  8. import android.widget.Toast
  9. import com.devindi.wallpaper.R
  10.  
  11. class CounterActivityMvvm : AppCompatActivity() {
  12.  
  13.     val viewModel = CounterViewModel()
  14.  
  15.     override fun onCreate(savedInstanceState: Bundle?) {
  16.         super.onCreate(savedInstanceState)
  17.  
  18.         val view = View(this)
  19.         view.id = R.id.increment
  20.  
  21.         setContentView(view)
  22.  
  23.         viewModel.counter.observe(this, Observer { t -> Toast.makeText(this@CounterActivityMvvm, "Counter: $t", Toast.LENGTH_SHORT).show() })
  24.         view.setOnClickListener{ viewModel.increment() }
  25.     }
  26. }
  27.  
  28. class CounterViewModel {
  29.  
  30.     private val model = CounterRepository()
  31.  
  32.     val counter = model.counter
  33.  
  34.     fun increment() {
  35.         model.increment()
  36.     }
  37. }
  38.  
  39. class CounterRepository {
  40.  
  41.     val counter = MutableLiveData<Int>()
  42.  
  43.     init {
  44.         counter.value = 0
  45.     }
  46.  
  47.     fun increment() {
  48.         counter.value = counter.value!! + 1
  49.     }
  50. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top