【问题标题】:Kotlin open AlertDialog in RecycleView item clickKotlin 在 RecycleView 项目中打开 AlertDialog 点击
【发布时间】:2019-06-17 19:59:16
【问题描述】:

我有RecyclerView,我想在单击RecyclerView 的项目时打开AlertDialog,我正在尝试遵循This java based Question 的概念,但它对我不起作用

我的适配器

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
    mRecyclerView.setHasFixedSize (true)
    mRecyclerView. layoutManager = LinearLayoutManager(this)

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

请给点建议

【问题讨论】:

    标签: android kotlin android-recyclerview


    【解决方案1】:

    Your code works我检查并复制了您的适配器代码,它可以工作,请将您的 xml 布局和启动该适配器的代码发送给我

    更新:

    您不能发送 applicationContext,您应该发送您的活动的上下文。 修复那个val adapter = OperationAdapter(this, arrayList)你不能在应用程序类中创建一个对话框,因为对话框应该附加到一个窗口,一个应用程序不是UI类并且没有窗口,所以它不能显示对话框。

    【讨论】:

    • 谢谢你我会发布xml代码请检查更新的问题
    • @JimaleAbdi 点击时你的应用会崩溃吗?
    • 是的,它开始崩溃了
    • @JimaleAbdi 我找到了解决方案。你不能发送 applicationContext,你应该发送你的活动的上下文。修复val adapter = OperationAdapter(this, arrayList) 您不能在应用程序类中创建对话框,因为对话框应该附加到窗口,应用程序不是 UI 类并且没有窗口,因此它不能显示对话框。
    • 谢谢它有效,请更新您的答案以再次帮助其他人,谢谢您帮助我
    【解决方案2】:

    首先,永远不要将你的 onclick 放在 onBindViewHolder 中。那不是一个好习惯。其次,如果您想在 item 上执行任何点击事件,您可以选择接口或将您的 item 点击监听器放在扩展 RecyclerView.ViewHolder 的 ViewHolder 类(内部类)中。

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

    使用此链接可以更好地了解使用接口 https://android.jlelse.eu/click-listener-for-recyclerview-adapter-2d17a6f6f6c9 与适配器进行交互

    【讨论】:

    • 感谢您的建议,我尝试了您的解决方案,但仍然无法正常工作,请检查更新后的问题,我在 onBindViewHolder 中删除了 onClick,但它是内部类
    • 您能否更好地解释或链接参考来解释为什么将其放在 onBindViewHolder 中不是一个好习惯的原因?我发现很多参考资料都是这样做的,而我个人是自己做的?提前致谢
    • @Shermano 我对此一无所知,我还需要知道为什么将它放在 onBindViewHolder 中不是一个好习惯?
    • 对不起@JimaleAbdi,我在问@MiniChip\
    【解决方案3】:

    你忘记了 create()

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

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2020-07-03
      • 2020-01-11
      • 2021-10-02
      • 2022-01-16
      • 1970-01-01
      • 2019-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多