Kotlin - How to get value from button choice in Dialog?

Viewed 176

I am in the process of creating a golf scorecard app. Every hole's score is an empty textView, and that textView has a setOnClickListener to open the score picker dialog. I want to get the score value from the score picker dialog.

Here is the dialog interface: https://i.stack.imgur.com/VNVc1.png

Each button is corresponding to a score.

I know that each button will need a setOnClickListener, but my knowledge is limited about everything else afterward.

So my question is how to return that score value so I can display it in that specific the textView and add it to the player's total? Any suggestions would be very helpful.

Thank you for your time.

3 Answers

You can implement custom listner, that listner you need to below code is for demo, implement in diaglog fragment

//score for current hole dialog
class SomeDialog:DialogFragment() {
    private var dl: CustomListener? = null

    // interface to detect dialog close event
    interface CustomListener {
        fun closeEvent(id: String, valueToPass: Int)
    }

    fun customListerTrig(l: CustomListener) {
        dl = l
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)


        button1.setOnClickListener {
            dl?.closeEvent("golfDiag", 1)
            this.dismiss()
        }
        button2.setOnClickListener {
            dl?.closeEvent("golfDiag", 2)
            this.dismiss()

        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_some_dialog, container, false)
    }

}

How i called and retrived button click event

 val common = SomeDialog()
     common.customListerTrig(object : CommonInfoDialog.CustomListener {
            override fun closeEvent(id: String, buttonValueFromDialog: Int) {
                // todo usebuttonValueFromDialog
            }
      })
      //use fragment according where this dialog will be called.
      common.show(this.childFragmentManager, "golfDiag")

May be this code is easy for you. Use Android dialog API to make your work easier.

   /**
     * e.g.
    showPickScoreDialog(requireContext()) { score ->
        Toast.makeText(context, "score[$score]", Toast.LENGTH_LONG).show()
    }
     */
    fun showPickScoreDialog(context: Context, callback: (Int) -> Unit) {
        val defaultCheckedScore = -1
        val scoreList = getScoreList()
        MaterialAlertDialogBuilder(context)
            .setTitle("Score For Current Hole:")
            .setSingleChoiceItems(scoreList, defaultCheckedScore) { dialog: DialogInterface?, which: Int ->
                val score = scoreList[which].toInt()
                callback(score)
                dialog?.dismiss()
            }
            .setNegativeButton("Cancel", null)
            .show()
    }

    private fun getScoreList(): Array<String> {
        val list = mutableListOf<String>()
        for (i in 1..12) {
            list.add(i.toString())
        }
        return list.toTypedArray()
    }

enter image description here

Related