【发布时间】:2018-08-28 16:37:00
【问题描述】:
我的数据库中存储了几个包含文件名的字符串数组。我想循环遍历这个数组以逐个返回存储在内部存储中的文件。数组之一的示例:
[["12","21","31"],["empty","22","32"],["13","23","33"]]// this is the array unmodified
下面是我现在拥有的代码,但只是给了我一个索引错误,因为索引在开始时是 12,因为数组从 12 开始。
layout = layout.replaceAll("\"empty\",?", "").replaceAll("[\"\\]\\
[\"]+","").replaceAll("^\"|\"$", ""); //this removes the "empty" string
String[] layoutArray = layout.split(",");
int rows = 3;
int columns = 3;
int layoutElement = 0;
try {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
// get the image from the internal storage
int imageIndex = Integer.valueOf(layoutArray[layoutElement]) - 1;
String imageFile = layoutArray[imageIndex];
Bitmap image = BitmapFactory.decodeFile(new File(getFilesDir(), imageFile).getAbsoluteFile().toString());
mImageList.add(new Grid(getApplicationContext(), i, j, image, imageFile));
layoutElement++;
}
}
} catch (Exception e) {
e.printStackTrace();
}
我知道我的代码在逻辑上完全错误,但我需要帮助,我无法理解它。每个数组值都有一个由该数字存储的文件名,我删除了“空”,因为它不需要。我的最终目标是将这些文件(即图像)放入网格视图中。
【问题讨论】:
-
粘贴错误日志
-
@Mohammad java.lang.ArrayIndexOutOfBoundsException: length=1;索引=12
标签: java android arrays local-storage