0

I have RecyclerView, i want to open AlertDialog when item of RecyclerView is Clicked, I'm trying to follow the concept of This java based Question but it didn't worked for me

My Adapter

class OperationAdapter (val context: Context,private val arrayList: ArrayList <Operations>):
RecyclerView.Adapter <OperationAdapter.Holder> () {

companion object {
    val TAG: String = OperationAdapter::class.java.simpleName
}

override fun onCreateViewHolder (parent: ViewGroup, viewType: Int): Holder {
    return Holder (LayoutInflater.from (parent.context ).inflate (R.layout.operaitemlist , parent, false))
}

override fun getItemCount (): Int = arrayList. size

override fun onBindViewHolder (holder: Holder, position: Int) {

    val opera = arrayList[position]
    holder.setData(opera, position)

}

inner class Holder (itemView: View): RecyclerView.ViewHolder (itemView) {

    private var currentOpera: Operations? = null
    private var currentPosition: Int = 0


    init {

        itemView.cardview.setOnClickListener {
            currentOpera?.let {

                AlertDialog.Builder(context)
                    .setTitle("My Title")
                    .setMessage("My Message")
                    .create()
                    .show()

            }
        }

        //the end of the init
    }

    //getting data from Operations and bind it into View
    fun setData(operation: Operations?, position: Int) {
        operation?.let {
            itemView.txtphonenumber.text = operation.phone
            itemView.txttime.text = operation.etime
        }

        this.currentOpera = operation
        this.currentPosition = position
    }
}

Set up the recycleview

//set up the recycleview
    mRecyclerView.setHasFixedSize (true)
    mRecyclerView. layoutManager = LinearLayoutManager(this)

          //adapter
          val adapter = OperationAdapter(applicationContext,arrayList)
                        adapter.notifyDataSetChanged()
                        mRecyclerView.adapter = adapter

Please any suggestion

Jimale Abdi
  • 2,574
  • 5
  • 26
  • 33

3 Answers3

2

Your code works I checked and reproduced your Adapter's code and it works, send me your xml layout and code of initiliazitation that Adapter

update:

You can't send applicationContext, you should send context of your activity. Fix on that val adapter = OperationAdapter(this, arrayList) You can not create a dialog within an application class since, the Dialog should be attached to a window, an Application is not UI class and has no window, so it can't show the dialog.

manwtein
  • 56
  • 4
  • Thank you i will post the xml code please check the updated question – Jimale Abdi Jun 17 '19 at 21:40
  • @JimaleAbdi does your app crash when you clicking? – manwtein Jun 17 '19 at 22:03
  • Yes it begins to crush – Jimale Abdi Jun 17 '19 at 22:06
  • @JimaleAbdi i found solution. you can't send applicationContext, you should send context of your activity. Fix on that `val adapter = OperationAdapter(this, arrayList)` **You can not create a dialog within an application class since, the Dialog should be attached to a window, an Application is not UI class and has no window, so it can't show the dialog.** – manwtein Jun 17 '19 at 22:08
  • @JimaleAbdi and about _never put your onclick in onBindViewHolder_ . Your method called "onBindViewHolder" and he must only bind your ViewHolder with your model(item) and nothing more. It do your code cleaner and readable, because you keep first princip SRP(Single responsibility princip). [NotBad example](https://antonioleiva.com/recyclerview-adapter-kotlin/) – manwtein Jun 17 '19 at 22:19
  • @JimaleAbdi do you still receive a crash? – manwtein Jun 17 '19 at 22:20
  • @JimaleAbdi it is the same. Try to find log about your exception and send me – manwtein Jun 17 '19 at 22:23
  • 1
    Thank you it works please update your answer to help the other again thank you for helping me – Jimale Abdi Jun 17 '19 at 22:35
1

First of all , never put your onclick in onBindViewHolder . Thats not a good practice. Secondly if you want to perform any click event on item , you can either go for interface or put your item click listener in ViewHolder class (internal class) that extends RecyclerView.ViewHolder .

item.setOnClickListenr{
  AlertDialog.Builder(this)
            .setTitle("My Title")
            .setMessage("My Message"))
            .setPositiveButton("Yes") { dialog, which -> todoFunctiononpositiveclick() }
            .setNegativeButton("No") { dialog, which -> dialog.dismiss() }
            .show()
 }

Use this link for better understanding on interaction with adapter using interface https://android.jlelse.eu/click-listener-for-recyclerview-adapter-2d17a6f6f6c9

Mini Chip
  • 949
  • 10
  • 12
  • Thank you for your suggestion i tried your solution but still it doesn't work, check the updated question i remove onClick in onBindViewHolder and but it Inner class – Jimale Abdi Jun 17 '19 at 20:53
  • Can you explain better or link an reference explaining the reason why isn't a good practice to put it within onBindViewHolder? I find a lot of references doing that and I personally do it myself? Thanks in advance – Shermano Jun 17 '19 at 21:35
  • @Shermano i have no idea about that also i need to know why isn't a good practice to put it within onBindViewHolder? – Jimale Abdi Jun 17 '19 at 21:55
  • I'm sorry @JimaleAbdi, I was asking to @MiniChip\ – Shermano Jun 17 '19 at 23:03
  • Here https://github.com/googlesamples/android-sunflower/blob/master/app/src/main/java/com/google/samples/apps/sunflower/adapters/GardenPlantingAdapter.kt some of the googel sample , and even in their tutorial they mentioned performing any click listener on inner class instead of onBind – Mini Chip Jun 18 '19 at 06:24
  • 1
    @Jimale Check this too https://stackoverflow.com/questions/33845846/why-is-adding-an-onclicklistener-inside-onbindviewholder-of-a-recyclerview-adapt – Mini Chip Jun 18 '19 at 06:32
0

You forgot the create()

val alertDialog = AlertDialog.Builder(context)
        .setTitle("My title")
        .setCancelable(true)
        .setMessage("My message")
        .create()

        alertDialog.show()

Hope it helps

Shermano
  • 1,100
  • 8
  • 31