我们怎样才能实现基本适配器与Android的科特林? [关闭](How can we imple

2019-10-28 20:09发布

我在科特林编程语言,如何使用Android的基础转接器来实现在科特林列表视图新。 任何一个能帮助我。

Answer 1:

检查这个代码

public class ListViewActivity() : AppCompatActivity() {

override fun onCreate(savedState: Bundle?) {
    super.onCreate(savedState)
    setContentView(R.layout.activity_list_view)

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

private class ListExampleAdapter(context: Context) : BaseAdapter() {
    internal var sList = arrayOf("Sunday", "Monday")
    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
    }
}


文章来源: How can we implement base adapter with kotlin in android? [closed]