Home > Net >  Android Studio spinner problem with ArrayListof kotlin
Android Studio spinner problem with ArrayListof kotlin

Time:10-21

//Model

Class DeviceBT
class DeviceBT(
    private val id: Long,
    private val name: String,
    private val macAddress: String
)

//MainActivity :

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

//Spinner Test
    var listDeviceBT = arrayListOf<DeviceBT>()
    listDeviceBT.add(BTDeviceModel(1,"Peripherique 1", "00:00:00:00:01" ))
    listDeviceBT.add(BTDeviceModel(2,"Peripherique 2", "00:00:00:00:02" ))
    listDeviceBT.add(BTDeviceModel(3,"Peripherique 3", "00:00:00:00:03" ))
    listDeviceBT.add(BTDeviceModel(4,"Peripherique 4", "00:00:00:00:04" ))

//spinner adapter
    val spinnerBTAdapter = ArrayAdapter<DeviceBTDataLight>(this, R.layout.support_simple_spinner_dropdown_item,listDeviceBTLight)
    spinnerBTAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item)
    spinner_BTAssocied.adapter = spinnerBTAdapter
}

the list of spinner don t return text, return Adapter.BTDeviceModel@456654e how to get the value of name ? Thank for your help

CodePudding user response:

Override toString method in DeviceBT class.

override fun toString(): String {
        return name
    }

CodePudding user response:

use Serializable it helps to Serializable data

class DeviceBT : Serializable (
    private val id: Long,
    private val name: String,
    private val macAddress: String
)

then call like this

 private val deviceBT: ArrayList<DeviceBT>
 val name = deviceBT.name 
  • Related