在适配器中重置数据时,列表在存储库中变为空

时间:2020-09-22 10:37:41

标签: android kotlin android-recyclerview kotlin-coroutines

我正在我的适配器中实现ActionMode.Callback

onActionItemClicked中,我调用片段中的method并传递一个列表:

override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
    if (item?.itemId == R.id.action_delete) {
        onItemClickListener.onDeleteItems(selectedItems)
        mode?.finish()
    }
    return true
}

当调用mode?.finish()时,此函数也调用了:

   override fun onDestroyActionMode(mode: ActionMode?) {
        selectedItems.clear()
        notifyDataSetChanged()
    }

我正在获取列表,并将其传递到存储库,当我进入CoroutineScope内部时,列表突然变成空的。 我以为CoroutineScope出了问题,但是如果我删除onDestroyActionMode方法主体,那一切都很好,我也可以删除我的项目。

我该如何解决我的代码,该代码可以删除我的项目 AND ,并关闭操作模式并清除数据而不会影响存储库功能中的列表(我不明白为什么会被执行?我只是在适配器中本地清除数据,它如何影响我的存储库功能?)

存储库中的函数如下:

    fun deleteItems(items: List<Items>) {
        Log.d(TAG, "deleteitems: before CoroutineScope $items") //-- All good
        CoroutineScope(Dispatchers.IO).launch {
            Log.d(TAG, "deleteitems: after CoroutineScope$items") //-- Empty list
            itemsDao.deleteitems(items)
        }
    }

0 个答案:

没有答案