Java 从文件室数据库Kotlin中删除项目
删除项目时,我收到以下错误java.lang.IndexOutOfBoundsException:索引:0,大小:0。第五行写着这个问题。我想我指的是数组之外的元素,但是Idk如何解决这个问题呢 删除操作:Java 从文件室数据库Kotlin中删除项目,java,android,sqlite,android-studio,kotlin,Java,Android,Sqlite,Android Studio,Kotlin,删除项目时,我收到以下错误java.lang.IndexOutOfBoundsException:索引:0,大小:0。第五行写着这个问题。我想我指的是数组之外的元素,但是Idk如何解决这个问题呢 删除操作: private fun deleteSwipeAction() { val swipeHandler = object : ItemSimpleTouch(requireContext()) { override fun onSwiped(viewHolder: Re
private fun deleteSwipeAction() {
val swipeHandler = object : ItemSimpleTouch(requireContext()) {
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
val position = viewHolder.absoluteAdapterPosition
val item = viewModel.products?.get(position)
val name = item?.name
adapter.deleteItem(position)
viewModel.deleteProduct(item)
showActionSnackbar(rv_product_list, "Вы удалили $name", "Востановить",
{ restoreDeletedItem(item, position)}, requireContext())
}
}
val itemTouchHelper = ItemTouchHelper(swipeHandler)
itemTouchHelper.attachToRecyclerView(rv_product_list)
}
视图模型:
class ProductViewModel : ViewModel() {
private val repository = ProductRepository()
val data: MutableLiveData<MutableList<Product>> = MutableLiveData()
var products: MutableList<Product>? = mutableListOf()
init {
subscribeToData()
repository.getProducts()
}
fun insertProduct(data: Product?) {
if (data != null) {
repository.insertProduct(data)
}
}
fun deleteProduct(data: Product?) {
if (data != null) {
repository.deleteProduct(data)
}
}
private fun subscribeToData() {
repository.data.observeForever {
data.value = it
products = data.value
}
}
class ProductViewModel:ViewModel(){
private val repository=ProductRepository()
val数据:MutableLiveData=MutableLiveData()
变量产品:MutableList?=mutableListOf()
初始化{
下标betodata()
repository.getProducts()
}
fun insertProduct(数据:产品?){
如果(数据!=null){
repository.insertProduct(数据)
}
}
产品(数据:产品?){
如果(数据!=null){
repository.deleteProduct(数据)
}
}
私人娱乐{
repository.data.observeForver{
data.value=it
产品=数据值
}
}
}