kotlin if(!YSDeviceUtils.getUSBStoragePath(0).isNullOrEmpty())mUsbAdapter?.addItem(File(YSDeviceUtils.getUSBStoragePath(0))) if(!YSDeviceUtils.getUSBStoragePath(1).isNullOrEmpty())mUsbAdapter?.addItem(File(YSDeviceUtils.getUSBStoragePath(1))) if(!YSDeviceUtils.getUSBStoragePath(2).isNullOrEmpty())mUsbAdapter?.addItem(File(YSDeviceUtils.getUSBStoragePath(2))) if(!YSDeviceUtils.getUSBStoragePath(3).isNullOrEmpty())mUsbAdapter?.addItem(File(YSDeviceUtils.getUSBStoragePath(3))) if(!YSDeviceUtils.getUSBStoragePath(4).isNullOrEmpty())mUsbAdapter?.addItem(File(YSDeviceUtils.getUSBStoragePath(4)))怎么拿下标更方便
时间: 2024-04-22 11:24:01 浏览: 74
在这种情况下,你可以使用一个循环来处理这个逻辑,而不是逐个检查每个下标。你可以使用一个简单的 for 循环来完成这个任务,如下所示:
```kotlin
for (i in 0..4) {
val storagePath = YSDeviceUtils.getUSBStoragePath(i)
if (!storagePath.isNullOrEmpty()) {
mUsbAdapter?.addItem(File(storagePath))
}
}
```
这样,你可以通过循环从 0 到 4 来获取每个下标,并检查相应的存储路径是否为空或 null。如果不为空,就将其添加到 mUsbAdapter 中。这种方法更简洁和可扩展,并且可以处理任意数量的下标。
阅读全文
相关推荐














