如何解决RecyclerView和数据库-java.lang.IndexOutOfBoundsException:检测到不一致无效的视图支架适配器
可以删除数据库中添加的第一个项目(最旧的项目),但是当我尝试删除数据库之间的项目或添加到数据库中的最新项目时,会发生以下错误。调用deletePlayerHistory()
函数可从数据库中删除项目。
如下图所示:
ITEM 1(数据库中添加的最新项目)->错误
项目2->错误
项目3->错误
项目4(数据库中添加的第一项)->可以删除
FriendHistoryAdapter.kt
class FriendHistoryAdapter(
private var friendHistoryData: List<FriendHistoryData>,private val friendHistoryDao: FriendHistoryDao,private val mContext: Context
) :
RecyclerView.Adapter<FriendHistoryAdapter.FriendHistoryHolder>(),CoroutineScope {
private val job = Job()
override val coroutineContext: CoroutineContext
get() =job + Dispatchers.Main
class FriendHistoryHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val textViewPlayer1: TextView = itemView.findViewById(R.id.text_view_player_one_name)
val textViewPlayer2: TextView = itemView.findViewById(R.id.text_view_player_second_name)
val textViewScore: TextView = itemView.findViewById(R.id.text_view_score)
val textViewWhoWon: TextView = itemView.findViewById(R.id.text_view_player_won)
val deleteImageButton: ImageView = itemView.findViewById(R.id.image_delete)
}
override fun onCreateViewHolder(parent: ViewGroup,viewType: Int): FriendHistoryHolder {
val itemView: View = LayoutInflater.from(parent.context)
.inflate(R.layout.friend_history_item,parent,false)
return FriendHistoryHolder(itemView)
}
@SuppressLint("SetTextI18n")
override fun onBindViewHolder(holder: FriendHistoryHolder,position: Int) {
holder.textViewPlayer1.text = friendHistoryData[position].playerOneName
holder.textViewPlayer2.text = friendHistoryData[position].playerSecondName
holder.textViewScore.text =
"Score: ${friendHistoryData[position].playerOneScore}-${friendHistoryData[position].playerSecondScore}"
when {
friendHistoryData[position].playerOneScore > friendHistoryData[position].playerSecondScore ->
holder.textViewWhoWon.text = "${friendHistoryData[position].playerOneName} won!"
friendHistoryData[position].playerOneScore < friendHistoryData[position].playerSecondScore ->
holder.textViewWhoWon.text =
"${friendHistoryData[position].playerSecondName} won!"
else -> holder.textViewWhoWon.text = "Draw!"
}
holder.deleteImageButton.setOnClickListener {
deletePlayerHistory(position)
}
}
override fun getItemCount(): Int {
return friendHistoryData.size
}
private fun deletePlayerHistory(position: Int) {
val item = friendHistoryData[position]
(friendHistoryData as MutableList).remove(item)
launch {
friendHistoryDao.deleteHistory(item)
Toast.makeText(mContext,"Item removed",Toast.LENGTH_SHORT).show()
}
notifyItemChanged(position)
}
}
VsFriendHistory.kt
class VsFriendHistory : BaseActivity() {
private lateinit var friendHistoryWholeData: List<FriendHistoryData>
private lateinit var friendRecyclerView: RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_vs_friend_history)
friendRecyclerView = findViewById(R.id.friend_recycler_view)
friendRecyclerView.layoutManager = LinearLayoutManager(this)
friendRecyclerView.setHasFixedSize(true)
launch{
lateinit var friendHistoryDao: FriendHistoryDao
val database: FriendHistoryDatabase? = application?.let {
FriendHistoryDatabase.getInstance(it)
}
if (database != null) {
friendHistoryDao = database.friendHistoryDao()
}
friendHistoryWholeData = friendHistoryDao.getWholeHistory()
friendRecyclerView.adapter = FriendHistoryAdapter(friendHistoryWholeData,friendHistoryDao,application)
if (friendHistoryWholeData.isEmpty()) {
Toast.makeText(baseContext,"No history to show",Toast.LENGTH_SHORT).show()
}
}
}
}
FriendHistoryData.kt
@Entity(tableName = "friend_history")
data class FriendHistoryData(
val playerOneName: String,val playerSecondName: String,val playerOneScore: Int,val playerSecondScore: Int
) {
@PrimaryKey(autoGenerate = true)
var id = 0
}
解决方法
好,我得到了答案
在 "compilerOptions": {
"skipLibCheck": true
}
类的notifyDataSetChanged()
块中以及在init
函数调用FriendHistoryAdapter.kt
和deletePlayerHistory()
中使用notifyItemRemoved(position)
来更新最终值的范围。清单。这就是更新包含数据库中项目的列表大小的问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。