0

I am using Anko in a basic Android app where I am implementing recyclerView. In the onCreateViewHolder() method I am getting a compile time error saying type Mismatch. Everything else is fine in the below code.

class ListAdapter(val arrayList: ArrayList<String> = ArrayList<String>()) : RecyclerView.Adapter<Holder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder? {

        //type Mismatch error required AnkoContext<ViewGroup> Found AnkoContext<Context>
        return Holder(ItemUI().createView(AnkoContext.create(parent!!.context)))

    }

    override fun onBindViewHolder(holder: Holder, position: Int) {
        val item = arrayList.get(position)
        holder.bind(item)
    }

    override fun getItemCount(): Int {
        return arrayList.size
    }

}

class ItemUI : AnkoComponent<ViewGroup> {
    override fun createView(ui: AnkoContext<ViewGroup>): View {
        return with(ui) {

            verticalLayout {
                lparams(width = matchParent, height = dip(48))
                horizontalPadding = dip(16)

                var name=textView {
                    id = 7
                    singleLine = true
                    textSize = 16f
                }
                name.onClick {
                    toast("Hi,  ${name.text}!")
                }
            }
        }
    }
}

class Holder(itemView: View) : RecyclerView.ViewHolder(itemView){
    val name: TextView = itemView.find(1)

    fun bind(nm: String) {
        name.text = nm
    }
}

Please let me know if I am using a wrong Syntax or the implementation of the recyclerview is erroneous

Ankul Jain
  • 35
  • 1
  • 9

1 Answers1

2

Sorry for late answer, but just browsing through my code for recyclerView adapter with Anko I noticed that there are creators for AnkoContext with following signatures only:

AnkoContext.create(ctx: Context, owner: ViewGroup, setContentView: Boolean = false)
AnkoContext.create(ctx: Context, setContentView: Boolean = false)

IDE (Android Studio) underlines the line when you're mistaken. I used the first one:

AnkoContext.create(parent!!.context, parent)
Antek
  • 721
  • 1
  • 4
  • 27