单击按钮从回收站视图中删除项目 - Kotlin MVVM Firestore [英] Delete item from recyclerview on button click - Kotlin MVVM Firestore

查看:31
本文介绍了单击按钮从回收站视图中删除项目 - Kotlin MVVM Firestore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户单击回收站视图中的删除按钮时,我无法从 Firestore 集合中删除数据.我可以毫无问题地从 recyclerview 中删除它,但是我无法在适配器、视图模型和处理 Firestore 操作的存储库之间建立连接.

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.

在我的适配器中,我从回收站视图中删除了用户点击的项目:

In my adapter, I remove the item the user clicked on from the recyclerview:

class ArticleAdapter : RecyclerView.Adapter<ArticleAdapter.ViewHolder>() {

var data = mutableListOf<Product>()
    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()
    }
} ...

在我的视图模型中查询 Firestore 集合后填充 recyclerview:

The recyclerview is populated after a query to the Firestore collection in my viewmodel:

class ArticleViewModel(private val repository: ProductRepository) : ViewModel() {

var savedProducts: MutableLiveData<MutableList<Product>> = MutableLiveData<MutableList<Product>>()

init {
    savedProducts = getProducts()
}

fun getProducts(): MutableLiveData<MutableList<Product>> {
    repository.getProducts().addSnapshotListener(EventListener<QuerySnapshot> { value, e ->
        if (e != null) {
            savedProducts.value = null
            return@EventListener
        }

        val savedProductsList: MutableList<Product> = mutableListOf()
        for (doc in value!!) {
            val item = doc.toObject(Product::class.java)
            item.id = doc.id
            savedProductsList.add(item)
        }
        savedProductsList.sortBy { i -> i.productName }
        savedProducts.value = savedProductsList
    })

    return savedProducts
}  }

在我的 Fragment 中,我观察了 savedProducts 可能发生的任何变化:

In my Fragment, I'm then observing any changes that might happen to savedProducts:

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
        }
    })

    ...
} }

有没有一种方法可以观察/保存适配器中已删除项目的 ID,并将该 ID 从适配器传输"到 UI,只要该 ID 字段为人口稠密?还是应该直接从适配器访问视图模型?不知怎的,这感觉有点不对……

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...

推荐答案

声明一个局部变量

var removedPosition : Int ? = null

然后将此变量更新为deleteButton的onClick事件

then update this variable into onClick event of deleteButton

holder.deleteButton.setOnClickListener {
        data.removeAt(position)
        removedPosition = position
        notifyDataSetChanged()
    }

请在 Adapter (ArticleAdapter) 中创建一个方法

Please make one method in Adapter (ArticleAdapter)

fun getRemoveItemPosition() : Int {
   var position = removedPosition
   return position;
}

返回已移除项的位置并在 UI(ArticleOverviewFragment) 中调用该方法,您需要从 recyclerview 中获取已移除项的位置

which return the position of removed Item and call that method in UI(ArticleOverviewFragment) where you will require to get position of removed item from recyclerview

var removedItemPosition = adapter.getRemoveItemPosition()

现在您将使用名为 removedItemPosition 的变量获取移除项目位置的值
<小时>因此,您可以在 UI 中获取已删除项目的位置,您可以在其中调用在视图模型 (ArticleViewModel) 中声明的函数来删除 Firestore 集合中的特定项目.

Now you will get value of remove item Position using variable called removedItemPosition


So You can get Position of removed Item in UI where you can call a function declared in the viewmodel (ArticleViewModel) to delete particular item in firestore collection.

这篇关于单击按钮从回收站视图中删除项目 - Kotlin MVVM Firestore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆