重新显示DialogFragment后检查选定的项目

时间:2019-01-05 17:00:44

标签: android kotlin dialog

我有一个DialogFragment,其中包含多项选择列表。当我检查某些项目并关闭并再次显示对话框时,不再检查它们。我该如何处理?我确保在重新显示对话框后没有清除selectedDays ArrayList,但是无论如何setItemChecked都无法工作。

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    return activity?.let {
        AlertDialog.Builder(it)
            .setMultiChoiceItems(R.array.days_of_week, null
            ) { _, which, isChecked ->
                if (isChecked && !selectedDays.contains(which))
                    selectedDays.add(which)
                else if (selectedDays.contains(which))
                    selectedDays.remove(which)
            }
            .setPositiveButton(R.string.confirm, null)
            .setNegativeButton(R.string.cancel
            ) { _, _ ->

            }
            .create().also { alertDialog ->
                val listView = alerDialog.listView
                for (day in selectedDays) {
                    listView.setItemChecked(day, true)
                }
            }
    } ?: throw IllegalStateException("Activity cannot be null")
}

0 个答案:

没有答案