转换ArrayList到arrayof在Kotlin android?



我有SingleChoiceItems对话框,我有列表不是数组我想把List转换成arrayof

MaterialAlertDialogBuilder(ctx)
.setTitle("Hello")
.setNeutralButton("Cancle") { dialog, which ->
// Respond to neutral button press
}
.setPositiveButton("Ok") { dialog, which ->
// Respond to positive button press
}
// Single-choice items (initialized with checked item)
.setSingleChoiceItems(?, checkedItem) { dialog, which ->
// Respond to item chosen
}
.show()

我解决了这个问题。

override fun onItemSwipeRight(position: Int) {
val phonesItems = getPhonesArray(position)
val checkedItem = 0
MaterialAlertDialogBuilder(ctx, R.style.MaterialAlertDialog_App)
.setTitle(ctx.getString(R.string.tit_phones_dialog, ctx.getString(R.string.str_sms)))
.setNeutralButton(ctx.getString(R.string.str_cancel)) { dialog, which ->
// Respond to neutral button press
}
.setPositiveButton(ctx.getString(R.string.str_ok)) { dialog, which ->
// Respond to positive button press
}
// Single-choice items (initialized with checked item)
.setSingleChoiceItems(phonesItems, checkedItem) { dialog, which ->
// Respond to item chosen
}
.show()
}

然后添加这个函数返回数组

private fun getPhonesArray(position: Int): Array<String?> {
val phonesList = arrayListOf<String>();
customerWithPhonesList[position].customerPhones.forEach { phone ->
phonesList.add("${phone.countryCode} ${phone.phoneNumber}");
}
return phonesList.toTypedArray()
}

使用这个扩展函数

inline fun <reified T> ArrayList<T>.toSingleArray() : Array<T>{
return Array(this.size) { i -> this[i] }
}

就像

一样
arrayListOf<String>("Hi","i","am","an","array").toSingleArray()

按此了解reified关键字Kotlin中的具体化关键字是如何工作的?

最新更新