```class MainActivity : AppCompatActivity(), Kodei...
# android
d
Copy code
class 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
    }
}
k
I think that notesAdapter is always null, as I don’t see any code that initialize this property.
e
Sure noteAdapter is null
d
@Kamil Kalisz what do you mean ?
k
you should instantiate notesAdapter
notesAdapter = NoteAdapter()
d
Oh, sorry i have seen that! Will be back.
c
@Derrick Wadek Maybe I didn't look through properly but I can't find where or how you inserting data in your view or any room callback. What data are you trying to display on the recycylerview. How does the data get into room?
d
Copy code
class 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)
            }
        }
    }
f
@Derrick Wadek, try to initialize your
initRecyclerView()
inside in the observer and before submit the data.