如何获取类型化数组中项目的索引?
How to get the index of an item in a typed array?
我想像这样将可绘制对象存储在资源数组中:
<integer-array name="sensor_icon_values">
<item>@drawable/sensor_brightness</item>
<item>@drawable/sensor_temperature</item>
<item>@drawable/sensor_humidity</item>
<item>@drawable/sensor_carbon_dioxide</item>
<item>@drawable/sensor_voltage</item>
</integer-array>
如何获取kotlin中某项的索引?
假设我想获取 resourceId 为 2131230874 的元素的索引。
我知道我可能必须使用这样的类型化数组:
val sensorIcons = resources.obtainTypedArray(R.array.sensor_icon_values)
这是适用于我的用例的解决方案:
val typedArray = resources.obtainTypedArray(R.array.sensor_icon_values)
for (i in 0..typedArray.length()){
if (selectedIconResourceId == typedArray.getResourceId(i,0)){
iconPreference?.setValueIndex(i)
break
}
}
typedArray.recycle()
我想像这样将可绘制对象存储在资源数组中:
<integer-array name="sensor_icon_values">
<item>@drawable/sensor_brightness</item>
<item>@drawable/sensor_temperature</item>
<item>@drawable/sensor_humidity</item>
<item>@drawable/sensor_carbon_dioxide</item>
<item>@drawable/sensor_voltage</item>
</integer-array>
如何获取kotlin中某项的索引? 假设我想获取 resourceId 为 2131230874 的元素的索引。
我知道我可能必须使用这样的类型化数组:
val sensorIcons = resources.obtainTypedArray(R.array.sensor_icon_values)
这是适用于我的用例的解决方案:
val typedArray = resources.obtainTypedArray(R.array.sensor_icon_values)
for (i in 0..typedArray.length()){
if (selectedIconResourceId == typedArray.getResourceId(i,0)){
iconPreference?.setValueIndex(i)
break
}
}
typedArray.recycle()