Johann
Johann

Reputation: 29885

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?

Upvotes: -1

Views: 2636

Answers (2)

Feedforward
Feedforward

Reputation: 4879

You should put superclass after class declaration:

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

    }
}

Upvotes: 1

Sergio
Sergio

Reputation: 30755

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.

Upvotes: 5

Related Questions