Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从文件室数据库Kotlin中删除项目_Java_Android_Sqlite_Android Studio_Kotlin - Fatal编程技术网

Java 从文件室数据库Kotlin中删除项目

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

删除项目时,我收到以下错误java.lang.IndexOutOfBoundsException:索引:0,大小:0。第五行写着这个问题。我想我指的是数组之外的元素,但是Idk如何解决这个问题呢

删除操作:

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
产品=数据值
}
}
}