title: RecyclerView and Database - java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter
body: <p>The first item(oldest item) added in the database can be deleted but when I try to delete a item from between or the latest item added in the database then the following error is occurring. <code>deletePlayerHistory()</code> function in called to delete the item from the database.<br>
  As illustrated below:</p>
  <p>ITEM 1(latest item added in the database) -&gt; Error <br>
  ITEM 2 -&gt; Error  <br>
  ITEM 3 -&gt; Error  <br>
  ITEM 4(First item added in the database) -&gt; Can be deleted <br></p>
  <p>FriendHistoryAdapter.kt</p>
  <pre><code>class FriendHistoryAdapter(
      private var friendHistoryData: List&lt;FriendHistoryData&gt;,
      private val friendHistoryDao: FriendHistoryDao,
      private val mContext: Context
  ) :
      RecyclerView.Adapter&lt;FriendHistoryAdapter.FriendHistoryHolder&gt;(), 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(&quot;SetTextI18n&quot;)
      override fun onBindViewHolder(holder: FriendHistoryHolder, position: Int) {
  
          holder.textViewPlayer1.text = friendHistoryData[position].playerOneName
          holder.textViewPlayer2.text = friendHistoryData[position].playerSecondName
          holder.textViewScore.text =
              &quot;Score: ${friendHistoryData[position].playerOneScore}-${friendHistoryData[position].playerSecondScore}&quot;
          when {
              friendHistoryData[position].playerOneScore &gt; friendHistoryData[position].playerSecondScore -&gt;
                  holder.textViewWhoWon.text = &quot;${friendHistoryData[position].playerOneName} won!&quot;
              friendHistoryData[position].playerOneScore &lt; friendHistoryData[position].playerSecondScore -&gt;
                  holder.textViewWhoWon.text =
                      &quot;${friendHistoryData[position].playerSecondName} won!&quot;
              else -&gt; holder.textViewWhoWon.text = &quot;Draw!&quot;
          }
          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, &quot;Item removed&quot;, Toast.LENGTH_SHORT).show()
          }
          notifyItemChanged(position)
      }
  }
  </code></pre>
  <p>VsFriendHistory.kt</p>
  <pre><code>class VsFriendHistory : BaseActivity() {
      private lateinit var friendHistoryWholeData: List&lt;FriendHistoryData&gt;
      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, &quot;No history to show&quot;, Toast.LENGTH_SHORT).show()
              }
          }
      }
  }
  </code></pre>
  <p>FriendHistoryData.kt</p>
  <pre><code>@Entity(tableName = &quot;friend_history&quot;)
  data class FriendHistoryData(
      val playerOneName: String,
      val playerSecondName: String,
      val playerOneScore: Int,
      val playerSecondScore: Int
  ) {
      @PrimaryKey(autoGenerate = true)
      var id = 0
  }
  </code></pre>