Android 奇怪的行为,试图删除项目时数组为空
因此,我将一些项目提取到我的数据库中,我将它们放在适配器中,但是当尝试删除它时,列表的大小是0,这很奇怪,因为当我点击一个元素删除列表时,实际上填充了一些项目Android 奇怪的行为,试图删除项目时数组为空,android,firebase,kotlin,Android,Firebase,Kotlin,因此,我将一些项目提取到我的数据库中,我将它们放在适配器中,但是当尝试删除它时,列表的大小是0,这很奇怪,因为当我点击一个元素删除列表时,实际上填充了一些项目 override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) fetchProducts(shopId) rv_my
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
fetchProducts(shopId)
rv_myshop.layoutManager = LinearLayoutManager(requireContext())
rv_myshop.addItemDecoration(
DividerItemDecoration(
requireContext(),
LinearLayoutManager.VERTICAL
)
)
}
private fun fetchProducts(shopId:String){
viewModel.fetchProducts(shopId).observe(viewLifecycleOwner, Observer { result ->
when(result){
is Resource.Loading -> { showProgress() }
is Resource.Success -> {
hideProgress()
adapter.setItems(result.data)
rv_myshop.adapter = adapter
if(adapter.itemCount == 0){
container_no_products.visibility = View.VISIBLE
}
}
is Resource.Failure -> { hideProgress() }
}
})
}
在这里一切正常之前,这段代码只会显示我的数据并填充我的列表!很好,但是现在,我尝试在适配器中按“删除”按钮删除一个项目,但该项目没有从数组中删除,当数组中有更多项目时,该数组显示0个项目
override fun onDeleteProductClick(position: Int) {
adapter.removeItem(position)
}
MyAdapter
var productsList=mutableListOf()
有趣的设置项(productsList:MutableList){
this.productsList=productsList
notifyDataSetChanged()
}
乐趣移除项(位置:Int){
productsList.removeAt(位置)
已删除的项目(位置)
}
因此,在这里,当我尝试removietem
时,它不会删除任何内容,因为列表的大小似乎为0,但实际上我以前使用setItems
我迷路了,我已经调试了程序,当我已经获取了值时,列表的大小显示为0
有什么提示吗?问题是,在我的数据源中,我正在修改此项,因此我只需要在删除结果时通知DataSetChanged
var productsList = mutableListOf<Product>()
fun setItems(productsList:MutableList<Product>){
this.productsList = productsList
notifyDataSetChanged()
}
fun removeItem(position: Int){
productsList.removeAt(position)
notifyItemRemoved(position)
}