DeviceListAdapter.kt 990 Bytes
Newer Older
Anon's avatar
Anon committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
package com.github.antweb.donkey

import android.bluetooth.BluetoothDevice
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.view.*

class DeviceListAdapter(context: Context, private val list: ArrayList<BluetoothDevice> = ArrayList()) :
    ArrayAdapter<BluetoothDevice>(context, android.R.layout.simple_list_item_1, list) {

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val view = if (convertView == null) {
            LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_1, parent, false)
        } else {
            convertView
        }

        val device = list[position]
        val label = "${device.name} (${device.address})"
        view.findViewById<TextView>(android.R.id.text1).text = label
        return view
    }
}