0

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

2 Answers 2

2

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()
    }
Sign up to request clarification or add additional context in comments.

Comments

0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.