我有一个 XML 文件,在我的资源中存储了一组短裤。我需要分配这个数组,但 Android API 只提供了一种获取整数数组的方法 (getResources.getIntArray()
)。我最终得到了一个整数数组,稍后将其转换为短裤数组,但这并不理想。
有没有办法从 Android 资源中获取一组短裤?如果没有,是否有更有效/更清晰的方法将 int 数组转换为 short 数组?我的代码目前看起来像这样:
int indicesInt[] = context.getResources().getIntArray(R.array.indices);
short indices[] = new short[indicesInt.length];
for (int i = 0; i < indicesInt.length; i++) {
indices[i] = (short)indicesInt[i];
}
最佳答案
AFAIK 无法直接检索短裤数组。看看是否可以使用 int[]
代替 short[]
是值得的。如果您正在使用图书馆或其他东西并且无法更改要求,那么您的示例几乎是最好的方法。警惕可能的溢出。
关于java - 如何有效地从 Android 上的资源中获取短数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/738708/