admin管理员组

文章数量:1405905

I m building an Android application with Kotlin. I m using a custom RecicleView to display a list of item in my Activity.

So this is my custom recicleview class:

class DeviceDiscoveredViewAdapter(context: Context, deviceDiscoveredList: List<BluetoothDevice>,
                                  pairedDevice: List<Device>) : ArrayAdapter<BluetoothDevice>(context, R.layout.device_dicovered_list_item, deviceDiscoveredList) {
    @SuppressLint("MissingPermission")
    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var view = convertView
        val data = getItem(position)
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.device_dicovered_list_item, parent, false)
        }
        val pathImage = view!!.findViewById<ImageView>(R.id.device_discovered_image)
        val name = view.findViewById<TextView>(R.id.device_discovered_name)
        val macAddress = view.findViewById<TextView>(R.id.device_discovered_mac_address)
        val paired = view.findViewById<TextView>(R.id.device_discovered_paired)

        if (data != null) {
            name.text = "${data.name ?: "N/A"}"
            macAddress.text = "${data.address ?: "N/A"}"
            if(data.getBondState() == BluetoothDevice.BOND_BONDED){
                paired.text = "Associato"
            }else{
                //devo verificare che non sia già stato associato.
                


            }
            //image handling
            if (data.name!=null && data.name.contains(context.getString(R.string.device_zephyr_abbreviation), ignoreCase = true)){
                pathImage.setImageResource(R.drawable.zephyr_pairing)
            }else if (data.name!=null && data.name.contains(context.getString(R.string.device_cnr_abbreviation), ignoreCase = true)){
                pathImage.setImageResource(R.drawable.sensore_cnr)
            }


        }

        return view
    }

I need to use pairedDevice field into fun getView. What can I change to use it?

I m building an Android application with Kotlin. I m using a custom RecicleView to display a list of item in my Activity.

So this is my custom recicleview class:

class DeviceDiscoveredViewAdapter(context: Context, deviceDiscoveredList: List<BluetoothDevice>,
                                  pairedDevice: List<Device>) : ArrayAdapter<BluetoothDevice>(context, R.layout.device_dicovered_list_item, deviceDiscoveredList) {
    @SuppressLint("MissingPermission")
    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var view = convertView
        val data = getItem(position)
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.device_dicovered_list_item, parent, false)
        }
        val pathImage = view!!.findViewById<ImageView>(R.id.device_discovered_image)
        val name = view.findViewById<TextView>(R.id.device_discovered_name)
        val macAddress = view.findViewById<TextView>(R.id.device_discovered_mac_address)
        val paired = view.findViewById<TextView>(R.id.device_discovered_paired)

        if (data != null) {
            name.text = "${data.name ?: "N/A"}"
            macAddress.text = "${data.address ?: "N/A"}"
            if(data.getBondState() == BluetoothDevice.BOND_BONDED){
                paired.text = "Associato"
            }else{
                //devo verificare che non sia già stato associato.
                


            }
            //image handling
            if (data.name!=null && data.name.contains(context.getString(R.string.device_zephyr_abbreviation), ignoreCase = true)){
                pathImage.setImageResource(R.drawable.zephyr_pairing)
            }else if (data.name!=null && data.name.contains(context.getString(R.string.device_cnr_abbreviation), ignoreCase = true)){
                pathImage.setImageResource(R.drawable.sensore_cnr)
            }


        }

        return view
    }

I need to use pairedDevice field into fun getView. What can I change to use it?

Share Improve this question asked Mar 7 at 8:34 bircastribircastri 2,16714 gold badges55 silver badges132 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 0

Just add private val before pairedDevice: List<BluetoothClass.Device> in constructor and boom you can now use pairedDevice in getView

So It will look like

add modifier val to pairedDevice:

class DeviceDiscoveredViewAdapter(context: Context, deviceDiscoveredList: List<BluetoothDevice>,
                                  val pairedDevice: List<Device>) : ArrayAdapter<BluetoothDevice>(context, R.layout.device_dicovered_list_item, deviceDiscoveredList)

kotlin:

class ExampleClass(val a:Int,b:Int){
    val count  = a + b
}

same java:

public class ExampleClass{
    public final int a;
    public final int count;
    public ExampleClass(int a,int b){
        this.a = a;
        this.count = a+b;
   }
}

Just use private val pairedDevice:List<Device> instead of pairedDevice:List<Device> inside the constructor of the class as it will become accessible in whole class.

本文标签: androidRecicleViewAdapter customStack Overflow