SHARE
TWEET

Untitled

a guest Mar 22nd, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class MainActivity : AppCompatActivity() {
  2.  
  3.     override fun onCreate(savedInstanceState: Bundle?) {
  4.         super.onCreate(savedInstanceState)
  5.         setContentView(R.layout.activity_main)
  6.  
  7.         subscribeUi()
  8.     }
  9.  
  10.     private fun subscribeUi() {
  11.         /**
  12.          * ์ด ๊ณผ์ •์€ DI(dependency injection)์œผ๋กœ ๋Œ€์ฒด๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
  13.          */
  14.         val dao = AppDatabase.getInstance(this).inputMsgDao()
  15.         val repository = InputMsgRepository.getInstance(dao)
  16.         val factory = MailViewModelFactory(repository)
  17.         var viewModel = ViewModelProviders.of(this, factory).get(MainViewModel::class.java)
  18.  
  19.         viewModel.inputMsgs.observe(this, Observer {
  20.             if (it == null || it.isEmpty())
  21.                 return@Observer
  22.  
  23.             var sb = StringBuffer()
  24.             for (data in it) {
  25.                 sb.append(data.msg).append("\n")
  26.             }
  27.  
  28.             tv_result.text = sb.toString()
  29.         })
  30.  
  31.         btn_input.setOnClickListener {
  32.             var input = et_input.text.toString()
  33.             if (input == null || input.isEmpty())
  34.                 return@setOnClickListener
  35.  
  36.             et_input.setText("")
  37.             viewModel.insertMsg(InputMsg(msg = input))
  38.         }
  39.     }
  40. }
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
 
Top