Dhia Shalabi
Dhia Shalabi

Reputation: 1540

Convert ArrayList into arrayof in Kotlin android?

I have SingleChoiceItems Dialog and I have List not arrayof and I want to convert this List into 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()

Upvotes: 0

Views: 108

Answers (2)

Dhia Shalabi
Dhia Shalabi

Reputation: 1540

I solved the problem with this.

 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()
    }

And simply add this function to return the array

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()
    }

Upvotes: 2

AkkixDev
AkkixDev

Reputation: 450

Use this extension function

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

And simply use it like

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

Follow this to learn about reified keyword How does the reified keyword in Kotlin work?

Upvotes: 0

Related Questions