簡體   English   中英

單擊按鈕時從 recyclerview 中刪除項目 - Kotlin MVVM Firestore

[英]Delete item from recyclerview on button click - Kotlin MVVM Firestore

當用戶單擊回收站視圖中的刪除按鈕時,我無法從 Firestore 集合中刪除數據。 我可以從 recyclerview 中刪除它而沒有任何問題,但是我無法在適配器、視圖模型和處理 Firestore 操作的存儲庫之間建立連接。

在我的適配器中,我從 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:

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 可能發生的任何變化:

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 的字段,我就會在視圖模型中調用 function 聲明? 或者我應該直接從適配器訪問視圖模型? 怎么說呢,感覺有點不對...

聲明一個局部變量

var removedPosition : Int ? = null

然后將此變量更新為 deleteButton 的 onClick 事件

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

請在 Adapter ( ArticleAdapter ) 中創建一種方法

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

它返回已刪除項目的 position 並在 UI( ArticleOverviewFragment )中調用該方法,您需要從 recyclerview 獲取已刪除項目的 position

var removedItemPosition = adapter.getRemoveItemPosition()

現在您將使用名為removedItemPosition的變量獲得刪除項目 Position 的值


因此,您可以在 UI 中獲取已刪除項目的 Position ,您可以在其中調用視圖模型( ArticleViewModel )中聲明的 function 以刪除 firestore 集合中的特定項目。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM