我试图在ListView中显示一个数组,但是我的MainActivity的最后两行会抛出一条Expecting member declaration
消息。知道为什么吗?
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>
答案 0 :(得分:1)
有一个例子here。
基本上,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
}
}
然后,将适配器包含在ListView
:
val lv = findViewById(R.id.list) as ListView
lv.adapter = ListExampleAdapter(this)
答案 1 :(得分:0)
以下适配器将满足您的需求
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
}
}
}
然后设置适配器
val adapter = MyArrayAdapter<String>(this authors);
listView.adapter = adapter;