https://kotlinlang.org logo
#android
Title
# android
d

Derrick Wadek

04/22/2020, 9:19 AM
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

Kamil Kalisz

04/22/2020, 9:25 AM
I think that notesAdapter is always null, as I don’t see any code that initialize this property.
e

Eric Ampire [MOD]

04/22/2020, 9:27 AM
Sure noteAdapter is null
d

Derrick Wadek

04/22/2020, 9:27 AM
@Kamil Kalisz what do you mean ?
k

Kamil Kalisz

04/22/2020, 9:28 AM
you should instantiate notesAdapter
notesAdapter = NoteAdapter()
d

Derrick Wadek

04/22/2020, 9:28 AM
Oh, sorry i have seen that! Will be back.
c

chidi

04/22/2020, 9:37 AM
@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

Derrick Wadek

04/22/2020, 10:07 AM
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

ferrugem

04/22/2020, 11:58 AM
@Derrick Wadek, try to initialize your
initRecyclerView()
inside in the observer and before submit the data.
5 Views