在Kotlin android中将ArrayList转换为arrayof?
我有SingleChoiceItems对话框,我有List not arrayof 我想把这个列表转换成arrayof在Kotlin android中将ArrayList转换为arrayof?,android,Android,我有SingleChoiceItems对话框,我有List not arrayof 我想把这个列表转换成arrayof MaterialAlertDialogBuilder(ctx) .setTitle("Hello") .setNeutralButton("Cancle") { dialog, which -> // Respond to neu
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()
使用此扩展功能
inline fun <reified T> ArrayList<T>.toSingleArray() : Array<T>{
return Array(this.size) { i -> this[i] }
}
inline fun ArrayList.toSingleArray():数组{
返回数组(this.size){i->this[i]}
}
简单地像这样使用它
arrayListOf<String>("Hi","i","am","an","array").toSingleArray()
arrayListOf(“Hi”、“i”、“am”、“an”、“array”)。toSingleArray()
按照此操作了解具体化的
关键字
使用此扩展功能
inline fun <reified T> ArrayList<T>.toSingleArray() : Array<T>{
return Array(this.size) { i -> this[i] }
}
inline fun ArrayList.toSingleArray():数组{
返回数组(this.size){i->this[i]}
}
简单地像这样使用它
arrayListOf<String>("Hi","i","am","an","array").toSingleArray()
arrayListOf(“Hi”、“i”、“am”、“an”、“array”)。toSingleArray()
按照此操作了解具体化的
关键字
我用这个解决了这个问题
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()
}
private fun getPhonesArray(位置:Int):数组{
val phonesList=arrayListOf();
customerWithPhonesList[position].customerPhones.forEach{phone->
添加(“${phone.countryCode}${phone.phoneNumber}”);
}
返回phonesList.toTypedArray()
}
我解决了这个问题
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()
}
private fun getPhonesArray(位置:Int):数组{
val phonesList=arrayListOf();
customerWithPhonesList[position].customerPhones.forEach{phone->
添加(“${phone.countryCode}${phone.phoneNumber}”);
}
返回phonesList.toTypedArray()
}