Poblando ListView desde array en Kotlin

Estoy tratando de mostrar una matriz en un ListView pero las últimas dos líneas de mi MainActivity arrojan un post de Expecting member declaration . ¿Alguna idea de por qué?

MainActivity.kt

 package com.example.apple.stories import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.widget.ArrayAdapter import android.widget.ListView import org.jetbrains.anko.db.MapRowParser class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } val authors = arrayOf("Conan Doyle, Arthur", "Christie, Agatha", "Collins, Wilkie"); val listView = ListView(this); val adapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, authors); listView.adapter = adapter; activity_main.addView(listView); } 

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.apple.stories.MainActivity"> <ListView android:fillViewport="true" android:id="@+id/listView" /> </android.support.constraint.ConstraintLayout> 

Hay un ejemplo aquí .

Básicamente, el código es muy similar en Java:

 class ListExampleAdapter(context: Context) : BaseAdapter() { internal var sList = arrayOf("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen") 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 } } 

Luego, para include el adaptador a su ListView :

 val lv = findViewById(R.id.list) as ListView lv.adapter = ListExampleAdapter(this) 

El adaptador siguiente será su necesidad

 class MyArrayAdapter(val contexts: Context, val array: ArrayList<String>) : ArrayAdapter<String>(contexts, android.R.layout.simple_list_item_1, array) { companion object { class Holder { var textView: TextView? = null } } override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View { var holder: Holder; if (convertView == null) { val view = LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_1, parent, false) holder = Holder() holder.textView = view.findViewById(android.R.id.text1) holder.textView?.text = array[position] view.tag = holder return view; } else { holder = convertView.tag as Holder holder.textView?.text=array[position] return convertView } } } 

A continuación, configure el adaptador

 val adapter = MyArrayAdapter<String>(this authors); listView.adapter = adapter;