title: How to update serial number text in recyclerview items in real time
body: <p>I have a RecyclerView that displays a series of numbers at the beginning of each list item as shown below.</p>
  
  <p><a href="https://i.stack.imgur.com/hEAuR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hEAuR.png" alt="enter image description here"></a></p>
  
  <p>I did this by getting the position of the item on the list and adding one.</p>
  
  <p><strong>onBindViewHolder</strong></p>
  
  <pre><code>    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
      val item = getItem(position)
      holder.bind(item!!, clickListener)
      holder.numberTextView.text = position.plus(1).toString()
  }
  </code></pre>
  
  <p>I want the numbers to update in real time when I delete an item on the list. As of now, when I delete items in the recyclerview, I get a list as shown below. I have to refresh the layout to get a sequential numbered list. How do I go about updating the numbers in real time.</p>
  
  <p><a href="https://i.stack.imgur.com/XRhDE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XRhDE.png" alt="enter image description here"></a></p>
  
  <p><strong>My Adapter</strong></p>
  
  <pre><code>    class Adapter( 
         private val clickListener: ClickListener) :
  ListAdapter&lt;DataItems, Adapter.ViewHolder&gt;(DiffCallback()) {
  
  class DiffCallback : DiffUtil.ItemCallback&lt;DataItems&gt;() {
      override fun areItemsTheSame(oldItem: DataItems, newItem: DataItems): Boolean {
          return oldItem.itemId == newItem.itemId
      }
  
      @SuppressLint("DiffUtilEquals")
      override fun areContentsTheSame(oldItem: OrderSummary, newItem: OrderSummary): Boolean {
          return oldItem == newItem
      }
  }
  
  fun getItemAtPosition(position: Int): DataItems {
      return getItem(position)
  }
  
  override fun onBindViewHolder(holder: ViewHolder, position: Int) {
      val item = getItem(position)
      holder.bind(item!!, clickListener)
      holder.umberTextView.text = position.plus(1).toString()
  }
  
  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
      return ViewHolder.from(parent)
  }
  
  class ViewHolder private constructor(val binding: ItemBinding) :
      RecyclerView.ViewHolder(binding.root) {
  
      val numberTextView = binding.orderItemListNumber
  
      fun bind(
          item: DataItems,
          clickListener: SummaryItemListener
      ) {
          binding.clickListener = clickListener
          binding.DataItems= item
          binding.executePendingBindings()
      }
  
      companion object {
          fun from(parent: ViewGroup): ViewHolder {
              val layoutInflater = LayoutInflater.from(parent.context)
              val binding =
                  ItemBinding.inflate(layoutInflater, parent, false)
              return ViewHolder(binding)
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>How I delete an item:</p>
  
  <pre><code>    override fun onSwiped(
          viewHolder: RecyclerView.ViewHolder, 
           direction: Int) {
              val position = viewHolder.adapterPosition
              val item = adapter.getItemAtPosition(position)
  
              if (direction == ItemTouchHelper.LEFT) {
                  val toast: Toast =
                      Toast.makeText(
                          context,
                          "1 item removed: ${item.name}",
                          Toast.LENGTH_SHORT
                      )
                  toast.setGravity(Gravity.CENTER, 0, 0)
                  toast.show()
            viewModel.delete(item) // Method to delete item from database
           } 
  </code></pre>
  
  <p><strong>Delete method in ViewModel</strong></p>
  
  <pre><code>    fun delete(summary: OrderSummary) {
      uiScope.launch {
          deleteItem(summary)
          }
      }
  
      private suspend fun deleteItem(orderSummary: OrderSummary) {
         withContext(Dispatchers.IO) {
              database.delete(orderSummary)
          }
      }
  </code></pre>