Home > Software engineering >  How to show RecyclerView in AlertDialog? Android Kotlin
How to show RecyclerView in AlertDialog? Android Kotlin

Time:12-23

I have a program entity, I want to show them in Alert Dialog with RecyclerView. I use data-binding like binding.programAdapter = adapter but I don't know how to bind adapter and recyclerview in layout. To clarify, I want to show items with recyclerview in Alert Dialog using Data-Binding or whatever it works. The only problem is I can't write //binding.programAlertAdapter = adapter.

Main Activity

val adapter = ProgramAlertAdapter(this, it)
                                //binding.programAlertAdapter = adapter
val mDialogView = LayoutInflater.from(this).inflate(R.layout.program_of_today_alert_dialog, null)
                                val db = AlertDialog.Builder(this,R.style.MyDialogTheme).setView(mDialogView).setTitle("Programs of Today")
                                    .show()

Adapter

    class ProgramAlertAdapter(
    var mContext: Context,
    var programList: List<Program>
) : RecyclerView.Adapter<ProgramAlertAdapter.CardViewHolder>() {
    inner class CardViewHolder(binding: AlertProgramCardDesignBinding): RecyclerView.ViewHolder(binding.root) {
        var binding: AlertProgramCardDesignBinding
        init {
            this.binding = binding
        }
    }

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CardViewHolder {
    val layoutInflater = LayoutInflater.from(mContext)
    val binding: AlertProgramCardDesignBinding = DataBindingUtil.inflate(layoutInflater,
        R.layout.alert_program_card_design, parent, false)
    return CardViewHolder(binding)
}

override fun onBindViewHolder(holder: CardViewHolder, position: Int) {
    val program = programList[position]
    val p = holder.binding
    p.programObject = program
}

override fun getItemCount(): Int {

       return programList.size
    }
}

Card Design

<data>
    <variable
        name="programObject"
        type="com.gorkemersizer.gymapp.data.entity.Program" />
</data>
...
<Cardview...
   <TextView
    android:text="@{programObject.program_name}"
...

Alert Dialog Design

...
<data>
    <variable name="ProgramAlertAdapter" type="com.gorkemersizer.gymapp.ui.adapters.ProgramAlertAdapter" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@ id/rvAlertProgram"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:adapter="@{ProgramAlertAdapter}"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
enter code here

Thank you in advance

CodePudding user response:

Delete android:adapter="@{programAlertAdapter}" from XML, because you can't use databinding in this situation, need to give it in different way. Use findViewById like below,

val adapter = ProgramAlertAdapter(this, it)
val mDialogView = LayoutInflater.from(this).inflate(R.layout.program_of_today_alert_dialog, null)
                                val rv = mDialogView.findViewById<RecyclerView>(R.id.rvAlertProgram)
                                rv.adapter = adapter
                                val db = AlertDialog.Builder(this,R.style.MyDialogTheme).setView(mDialogView).setTitle("Programs of Today")
                                    .show()
  • Related