android - RecyclerView 和数据库 - java.lang.IndexOutOfBoundsException:检测到不一致。无效的视图支架适配器
问题描述
可以删除数据库中添加的第一个项目(最旧的项目),但是当我尝试从中删除项目或数据库中添加的最新项目时,会发生以下错误。deletePlayerHistory()
调用函数以从数据库中删除项目。
如下图所示:
项目 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
}
解决方案
好的,我得到了答案在类和函数调用中的块中
使用并更新最终列表的范围。这就是更新包含数据库中项目的列表大小的问题。notifyDataSetChanged()
init
FriendHistoryAdapter.kt
deletePlayerHistory()
notifyItemRemoved(position)
notifyItemRangeChanged(position,friendHistoryData.size)
推荐阅读
- c - 使用动态内存分配的矩阵加法和减法
- reactjs - TypeError:无法读取未定义的属性“组”
- java - 您能否通过 java 使用存根类为带有承载令牌的 Axis 客户端 SOAP 调用提供示例?
- pandas - 熊猫在一列中合并具有相同值的行
- barcode - Odoo POS 多条码扫描
- anylogic - 将数据库链接到动态变量 AnyLogic
- ruby-on-rails - 我在测试我的 Rails 应用程序模型时遇到错误
- javascript - 如何将按钮添加到表中的每一行并单击特定行按钮,只有该按钮必须使用 html jQuery php 隐藏
- nginx - Nginx 与 80 上的默认 tcp 冲突
- vue.js - 如何在 vuejs 中使用 axios 访问 json 文件?