我试图在我的 Android 应用程序中使用 Kotlin: https://github.com/bidrohi/KotlinListView复制以下 ListView。
不幸的是,我得到了一个错误,我无法解决自己。 这是我的密码:
MainActivity.kt:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val listView = findViewById(R.id.list) as ListView
listView.adapter = ListExampleAdapter(this)
}
private class ListExampleAdapter(context: Context) : BaseAdapter() {
internal var sList = arrayOf("Eins", "Zwei", "Drei")
private val mInflator: LayoutInflater
init {
this.mInflator = LayoutInflater.from(context)
}
override fun getCount(): Int {
return sList.size
}
override fun getItem(position: Int): Any {
return sList[position]
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View? {
val view: View?
val vh: ListRowHolder
if(convertView == null) {
view = this.mInflator.inflate(R.layout.list_row, parent, false)
vh = ListRowHolder(view)
view.tag = vh
} else {
view = convertView
vh = view.tag as ListRowHolder
}
vh.label.text = sList[position]
return view
}
}
private class ListRowHolder(row: View?) {
public val label: TextView
init {
this.label = row?.findViewById(R.id.label) as TextView
}
}
}
布局和这里一模一样: https://github.com/bidrohi/KotlinListView/tree/master/app/src/main/res/layout
我得到的完整错误消息是: 错误: (92,31)类型推断失败: 在 fun findViewById (p0: Int) : T 中没有足够的信息来推断参数 T! 请明确指定。
如果你能帮我,我会很感激的。