CodingDosaMooDosa
CodingDosaMooDosa

Reputation: 45

how to deal with spinner's selected item(android studio, kotlin)

I want to use spinner in a dialog for user-input like this.

enter image description here

I implemented spinner in a input dialog, but I don't know how to deal with the selected item of spinner. My goal is, if an user selected category in the spinner and write the input and press the confirm button, then add it to proper recyclerView.

My class for dialog is as below.

class inputDialog(context: Context) { private val dialog = Dialog(context) private lateinit var onClickListener : onDialogClickListener var spinnerList = arrayOf("운동","학과공부","개인공부 및 진로","기타")

fun setOnClickListener(listener : onDialogClickListener){
    onClickListener = listener
}

fun showDialog(){

    dialog.setContentView(R.layout.input_dialog)
    dialog.window!!.setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT)
    dialog.setTitle("오늘의 할 일을 입력해주세요")
    dialog.setCancelable(true)
    dialog.setCanceledOnTouchOutside(true)
    dialog.show()

    val input_editText = dialog.findViewById<EditText>(R.id.inputDlg_editText)
    dialog.spinner.adapter = spinner_adapter


    dialog.confirmBtn.setOnClickListener {
        onClickListener.onClicked(input_editText.text.toString())

        dialog.dismiss()
    }
}



interface onDialogClickListener{
    fun onClicked(input:String)
}

val spinner_adapter = ArrayAdapter(context,android.R.layout.simple_spinner_dropdown_item,spinnerList)

}

I need your help:(

Upvotes: 0

Views: 316

Answers (1)

Niaj Mahmud
Niaj Mahmud

Reputation: 469

implement in onCreate method

 spinner.onItemSelectedListener =
        object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View,
                position: Int,
                id: Long
            ) {

                val cattype = arrayCategory!![position].toString()
                Toast.makeText(
                    context,
                    arrayCategory!![position].toString() + " selected",
                    Toast.LENGTH_SHORT
                ).show()
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {}
        }

Upvotes: 0

Related Questions