【发布时间】:2020-05-20 13:51:34
【问题描述】:
我是 Android 版 Kotlin 的初学者。我有一些愚蠢的问题我找不到答案。
第一个问题。例如我有这样的代码
class myAdapter(context: Context, val list: List<DataSource>)
: ArrayAdapter<DataSource>(context, 0, list){
如果我这样写(不带参数)
class myAdapter(context: Context, val list: List<DataSource>)
: ArrayAdapter<DataSource>(){
然后 intellij 给我这个错误:以下函数都不能用提供的参数调用。我为什么要向 ArrayAdapter 提供参数以及它们会受到什么影响?
第二个问题。例如,我有一个代码:
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val lf: LayoutInflater = LayoutInflater.from(context)
val row = lf.inflate(R.layout.custom_list_view, parent, false)
val view_icon = row.findViewById<ImageView>(R.id.icon)
val view_title = row.findViewById<TextView>(R.id.title)
val view_subtitle = row.findViewById<TextView>(R.id.subtitle)
val ds: DataSource = list[position]
view_icon.setImageResource(R.drawable.ic_folder_black_24dp)
view_title.text = ds.ds_title
view_subtitle.text = ds.ds_subtitle
return row
}
“行”变量是什么意思,我为什么要给它分配“膨胀”?怎么用文字解释?如果我不分配,那么 inflate 将不起作用。 这是什么意思:
list[position]
我在哪里可以读到它? Kotlin 让我崩溃了。
【问题讨论】:
-
虽然您专注于 Android 开发,但了解 Kotlin 语言会对您有很大帮助:kotlinlang.org/docs/reference