ListAdapter not updating when editing content

爱⌒轻易说出口 提交于 2020-01-06 05:43:08

问题


Every time content is added to the recyclerview and it happens the title is misspelled when I go to fix the mistake, the update/edit does not take effect.

The goal is fixing the update error so when I go back to fix it, the content title would be updated

companion object
{  
//THIS THE ADAPTER CLASS
    private val DIFF_CALLBACK: DiffUtil.ItemCallback<Book> = object:DiffUtil.ItemCallback<Book>()
    {
        @Override
        override fun areItemsTheSame(oldItem: Book, newItem: Book):Boolean
        {
            return oldItem.id == newItem.id
        }

        @Override
        override fun areContentsTheSame(oldItem: Book, newItem: Book): Boolean
        {
            return oldItem.title == newItem.title &&
                    oldItem.author == newItem.author &&
                    oldItem.genre == newItem.genre

        }
    }
}

//THIS IS IN THE MAINACTIVITY
val adapter = BookAdapter()
recyclerView.adapter = adapter 

bookViewModel = 
ViewModelProviders.of(this).get(BookViewModel::class.java)
bookViewModel.getAllBooks().observe(this, object:Observer<List<Book>>
{
     @Override
     override fun onChanged(@Nullable books: List<Book>)
     {
         adapter.submitList(books)
     }
})

I expect the contents in the recyclerview to be updated if I want to fix the error which was misspelled.


回答1:


adapter.notifyDataSetChanged();

This worked in Java, but don't know kotlin syntax. Try adding this in onChange() method after

adapter.submitList(books)


来源:https://stackoverflow.com/questions/55443109/listadapter-not-updating-when-editing-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!