Derrick Wadek
04/22/2020, 9:19 AMclass MainActivity : AppCompatActivity(), KodeinAware {
override val kodein by kodein()
private var notesAdapter: NotesAdapter ?= null
private var mLayout : LinearLayoutManager ?= null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
ft_btn.setOnClickListener {
val intent = Intent(this, AddNoteActivity::class.java)
startActivity(intent)
}
initRecyclerview()
val db = NoteRoomDatabase(this)
val repo = NotesRepository(db)
val factory = NotesViewModelFactory(repo)
val notesViewModel : NotesViewModel = ViewModelProvider(this, factory).get(NotesViewModel::class.java)
notesViewModel.getAllNotesFromDB().observe(this, Observer {
notesAdapter?.submitList(it)
})
}
private fun initRecyclerview(){
recyclerView.setHasFixedSize(true)
mLayout = LinearLayoutManager(this)
recyclerView.layoutManager = mLayout
recyclerView.adapter = notesAdapter
}
}
Kamil Kalisz
04/22/2020, 9:25 AMEric Ampire [MOD]
04/22/2020, 9:27 AMDerrick Wadek
04/22/2020, 9:27 AMKamil Kalisz
04/22/2020, 9:28 AMDerrick Wadek
04/22/2020, 9:28 AMchidi
04/22/2020, 9:37 AMDerrick Wadek
04/22/2020, 10:07 AMclass AddNoteActivity : AppCompatActivity() {
private var notesViewModel: NotesViewModel?= null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add_note)
btn_save_note.setOnClickListener {
saveNotesToDB()
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}
}
private fun saveNotesToDB(){
val title = et_title.toString().trim()
val note = et_note_body.toString().trim()
when {
title.isEmpty() -> {
et_title.error = "Title cannot be blank!"
et_title.requestFocus()
}
note.isEmpty() -> {
et_note_body.error = "Body cannot be blank!"
et_note_body.requestFocus()
}
else -> {
val notes = Note(title, note)
notesViewModel?.insertNotesToDB(notes)
}
}
}
ferrugem
04/22/2020, 11:58 AMinitRecyclerView()
inside in the observer and before submit the data.