Java Android-使用数组将图像动态添加到列表中
我的数据库中存储了几个包含文件名的字符串数组。我想循环这个数组,以逐个返回存储在内部存储器中的文件。其中一个阵列的示例:Java Android-使用数组将图像动态添加到列表中,java,android,arrays,local-storage,Java,Android,Arrays,Local Storage,我的数据库中存储了几个包含文件名的字符串数组。我想循环这个数组,以逐个返回存储在内部存储器中的文件。其中一个阵列的示例: [["12","21","31"],["empty","22","32"],["13","23","33"]]// this is the array unmodified 下面是我现在的代码,但只是给了我一个索引错误,因为索引在开始时是12,因为数组从12开始 layout = layout.replaceAll("\"empty\",?", "").replaceAll
[["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();
}
layout=layout.replaceAll(“\”空“,?”,”).replaceAll(“[\”\]\”
[\“]+”,”).replaceAll(“^\”|\“$”,”)//这将删除“空”字符串
字符串[]layoutArray=layout.split(“,”);
int行=3;
int列=3;
int-layoutElement=0;
试一试{
对于(int i=0;i
我知道我的代码在逻辑上是完全错误的,但我需要这方面的帮助,我的头脑无法理解它。每个数组值都有一个由该数字存储的文件名,我删除了“empty”,因为它不是必需的。我的最终目标是将这些文件(即图像)放置到网格视图中。- 您正在使用“,”拆分文本,它将数组(作为示例提供)拆分为9个元素。。。您需要将所有“]、[”替换为类似“]-[”的内容,并使用“-”拆分字符串
- 您正在为每个嵌套循环增加
的值,而不在第一个循环中重置它>> 此代码应按预期工作layoutElement
layout = layout.replaceAll("\"empty\",?", "").replaceAll("^\"|\"$", "").replaceAll("\\],\\[", "\\]-\\["); String[] layoutArray = layout.split("-"); try { for (int i = 0; i < layoutArray.length; i++) { layoutArray[i]= layoutArray[i].replaceAll("[\\[\"\\]]",""); String[] splitted = layoutArray[i].split(","); for (int j = 0; j < splitted.length; j++) { int imageIndex = Integer.valueOf(splitted[j]) - 1; String imageFile = splitted[imageIndex]; Bitmap image = BitmapFactory.decodeFile(new File(getFilesDir(), imageFile).getAbsoluteFile().toString()); mImageList.add(new Grid(getApplicationContext(), i, j, image, imageFile)); } } } catch (Exception e) { e.printStackTrace(); }
layout=layout.replaceAll(“\”空“,?”,”).replaceAll(“^\”\”\“$”,”).replaceAll(“\\]”,\\[”,“\\]-\\[”; 字符串[]layoutArray=layout.split(“-”); 试一试{ 对于(int i=0;i
layout = layout.replaceAll("\"empty\",?", "").replaceAll("^\"|\"$", "").replaceAll("\\],\\[", "\\]-\\[");
String[] layoutArray = layout.split("-");
try {
for (int i = 0; i < layoutArray.length; i++) {
layoutArray[i]= layoutArray[i].replaceAll("[\\[\"\\]]","");
String[] splitted = layoutArray[i].split(",");
for (int j = 0; j < splitted.length; j++) {
int imageIndex = Integer.valueOf(splitted[j]) - 1;
String imageFile = splitted[imageIndex];
Bitmap image = BitmapFactory.decodeFile(new File(getFilesDir(), imageFile).getAbsoluteFile().toString());
mImageList.add(new Grid(getApplicationContext(), i, j, image, imageFile));
}
}
} catch (Exception e) {
e.printStackTrace();
}