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.ktdeletePlayerHistory()中使用notifyItemRemoved(position)来更新最终值的范围。清单。这就是更新包含数据库中项目的列表大小的问题。