title: Delete item from recyclerview on button click - Kotlin MVVM Firestore
body: <p>I'm having trouble with deleting data from my Firestore collection when the user clicks a delete button in a recyclerview. I can delete it from the recyclerview without any problems, but I'm having trouble to make the connection between the adapter, the viewmodel and the repository that handles Firestore operations.</p>
  
  <p>In my adapter, I remove the item the user clicked on from the recyclerview:</p>
  
  <pre><code>class ArticleAdapter : RecyclerView.Adapter&lt;ArticleAdapter.ViewHolder&gt;() {
  
  var data = mutableListOf&lt;Product&gt;()
      set(value) {
          field = value
          notifyDataSetChanged()
      }
  
  override fun getItemCount() = data.size
  
  override fun onBindViewHolder(holder: ViewHolder, position: Int) {
      val item = data[position]
      holder.bind(item)
  
      holder.deleteButton.setOnClickListener {
          data.removeAt(position)
          notifyDataSetChanged()
      }
  } ...
  </code></pre>
  
  <p>The recyclerview is populated after a query to the Firestore collection in my viewmodel:</p>
  
  <pre><code>class ArticleViewModel(private val repository: ProductRepository) : ViewModel() {
  
  var savedProducts: MutableLiveData&lt;MutableList&lt;Product&gt;&gt; = MutableLiveData&lt;MutableList&lt;Product&gt;&gt;()
  
  init {
      savedProducts = getProducts()
  }
  
  fun getProducts(): MutableLiveData&lt;MutableList&lt;Product&gt;&gt; {
      repository.getProducts().addSnapshotListener(EventListener&lt;QuerySnapshot&gt; { value, e -&gt;
          if (e != null) {
              savedProducts.value = null
              return@EventListener
          }
  
          val savedProductsList: MutableList&lt;Product&gt; = mutableListOf()
          for (doc in value!!) {
              val item = doc.toObject(Product::class.java)
              item.id = doc.id
              savedProductsList.add(item)
          }
          savedProductsList.sortBy { i -&gt; i.productName }
          savedProducts.value = savedProductsList
      })
  
      return savedProducts
  }  }
  </code></pre>
  
  <p>In my Fragment, I'm then observing any changes that might happen to savedProducts:</p>
  
  <pre><code>class ArticleOverviewFragment : Fragment(), KodeinAware {
  override val kodein: Kodein by kodein()
  private val factory: ArticleViewModelFactory by instance()
  private lateinit var viewModel: ArticleViewModel
  
  override fun onCreateView(
      inflater: LayoutInflater, container: ViewGroup?,
      savedInstanceState: Bundle?
  ): View? {
      val binding: FragmentArticleOverviewBinding =
          DataBindingUtil.inflate(inflater, R.layout.fragment_article_overview, container, false)
  
      viewModel = ViewModelProviders.of(this, factory).get(ArticleViewModel::class.java)
  
      binding.viewModel = viewModel
  
      val adapter = ArticleAdapter()
      binding.recyclerViewGoods.adapter = adapter
  
      viewModel.savedProducts.observe(viewLifecycleOwner, Observer {
          it?.let {
              adapter.data = it
          }
      })
  
      ...
  } }
  </code></pre>
  
  <p>Is there a way that I can observe/save the ID of the deleted item in my adapter and "transfer" that ID from the adapter to the UI where I call a function declared in the viewmodel whenever that field holding the ID is populated? Or should I directly access the viewmodel from the adapter? Somehow, that feels kinda wrong...</p>