在Kotlin中,这是我写的代码,但在二级构造函数this()处显示错误。
class MyCustomAdapter(context: Context, countryModelsList:ArrayList<CountryModel>) :
ArrayAdapter<CountryModel>(context,R.layout.list_coustom_item,countryModelsList ) {
constructor() : this()
错误是 "有周期性的调用链"
正如tynn所建议的 此处,
如果这个类有一个主构造函数,那么每个次要构造函数需要直接或间接地通过另一个次要构造函数委托给主构造函数。委托给同一类的另一个构造函数是通过this关键字来完成的。
我无法理解你想解决的具体用例,但我想下面的代码会有帮助。
class MyCustomAdapter(context: Context, resourceId: Int, countryModelsList:ArrayList<CountryModel>) :
ArrayAdapter<CountryModel>(context, resourceId, countryModelsList ) {
constructor(context: Context, countryModelsList: ArrayList<CountryModel>) : this(context, R.layout.list_coustom_item, countryModelsList)
constructor(context: Context): this(context, emptyList<CountryModel>())
}