目前,我必须为if (mBluetoothDef.isChecked) { aMutableList.add(mBluetoothDef) }
撰写完整的代码。
我想也许在Kotlin有一个简单的方法可以做到这一点。我该怎么办?
var aMutableList= mutableListOf<DeviceDef>()
var mBluetoothDef=BluetoothHelper(mContext).getBluetooth()
var mWiFiDef= WiFiHelper(mContext).getWiFi()
if (mBluetoothDef.isChecked) { aMutableList.add(mBluetoothDef) } // Simple way?
if (mWiFiDef.isChecked) {aMutableList.add(mWiFiDef)} //Simple way?
interface DeviceDef
data class BluetoothDef(
val isChecked: Boolean = true,
val status: Boolean = false
) : DeviceDef
data class WiFiDef(
val isChecked: Boolean = true,
val name: String,
val status: Boolean = false
) : DeviceDef
答案 0 :(得分:-1)
强烈建议在表达式中使用,如果需要甚至几个if:
when {
mBluetoothDef.isChecked -> aMutableList.add(mBluetoothDef)
mWiFiDef.isChecked -> aMutableList.add(mWiFiDef)
else -> {}
}