Search code examples
androidkotlinconstructorandroid-adapterclass-constructors

Using a secondary constructor in Kotlin


I get the error:

Expecting member declaration

class MyAdapter(val context: Context)  {
    constructor(context: Context,  itemInfos: List<ItemInfo>): RecyclerView.Adapter<ContentItemViewHolder> {

    }
}

What am I doing wrong?


Solution

  • Do something like this:

    class MyAdapter(val context: Context): RecyclerView.Adapter<ContentItemViewHolder>() {
        constructor(context: Context,  itemInfos: List<ItemInfo>): this(context) {
    
        }
    }
    

    If you inherit from another class you should specify it in class declaration, not constructor declaration.