【问题标题】:Why I can't remove element of arraylist为什么我不能删除数组列表的元素
【发布时间】:2019-11-02 09:03:04
【问题描述】:
class ScheduleUpdateDialog(private val schedules: ArrayList<Schedule>)
    : DialogFragment() {
    ...
    inner class DialogListener : DialogInterface.OnClickListener {
        override fun onClick(dialog: DialogInterface?, which: Int) {
            when(which) {
                DialogInterface.BUTTON_POSITIVE -> {
                    val content = calendarScheduleText.text.toString()
                    if(content != "") {
                        db.collection("schedules").document(id!!)
                            .update("content", content)
                        schedule.content = content
                        mainActivity.onDialogClick()
                    }
                }
                DialogInterface.BUTTON_NEUTRAL -> {
                    db.collection("schedules").document(id!!)
                        .delete()
                    schedules.removeAt(index!!)
                    mainActivity.onDialogClick()
                }
            }
        }
    }
}

【问题讨论】:

  • 可以ArrayList 中删除元素。为什么你认为你不能?
  • 如果遇到错误可以分享一下吗

标签: android kotlin arraylist


【解决方案1】:

您可以检查已删除元素的返回值。

abstract fun removeAt(index: Int): E

从列表中删除指定索引处的元素。

返回被移除的元素。

【讨论】:

    猜你喜欢
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    相关资源
    最近更新 更多