我正在尝试使用Kotlin在我的Android应用程序中复制以下ListView:https://github.com/bidrohi/KotlinListView .
不幸的是我收到了一个错误,我无法解决自己 . 这是我的代码:
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
我得到的完整错误消息是: Error:(92, 31) Type inference failed: Not enough information to infer parameter T in fun findViewById(p0: Int): T! Please specify it explicitly.
我很感激我能得到任何帮助 .
5 回答
您必须使用API级别26(或更高级别) . 此版本更改了
View.findViewById()
的签名 - 请参阅此处https://developer.android.com/preview/api-overview.html#fvbi-signature所以在你的情况下,
findViewById
的结果是不明确的,你需要提供类型:1 /改变
val listView = findViewById(R.id.list) as ListView
来val listView = findViewById<ListView>(R.id.list)
2 /改变
this.label = row?.findViewById(R.id.label) as TextView
来this.label = row?.findViewById<TextView>(R.id.label) as TextView
请注意,在2 /中只需要强制转换,因为
row
可以为空 . 如果label
也可以为空,或者如果您使row
不可为空,则不需要它 .Andoid O改变了findViewById api
至
所以,如果你是API 26的目标,你可以改变
至
要么
Its Working
API Level 25 or below use this
API Level 26 or Above use this
快乐的编码!
将您的代码更改为此 . 发生主要变化的地方标有星号 .
我建议你使用
synthetics
kotlin Android扩展程序:https://kotlinlang.org/docs/tutorials/android-plugin.html
https://antonioleiva.com/kotlin-android-extensions/
在您的情况下,代码将是这样的:
就如此容易 ;)