每次将内容添加到recyclerview时,碰巧我要更正错误时标题都拼错了,更新/编辑不会生效。
目标是修复更新错误,因此当我再次修复它时,内容标题将被更新
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)
}
})
如果我想修复拼写错误的错误,我希望recyclerview中的内容会更新。
答案 0 :(得分:0)
adapter.notifyDataSetChanged();
这在Java中有效,但是不知道kotlin语法。 尝试在
之后添加到onChange()方法中adapter.submitList(books)