Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android-使用数组将图像动态添加到列表中_Java_Android_Arrays_Local Storage - Fatal编程技术网

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

粘贴错误log@Mohammadjava.lang.ArrayIndexOutOfBoundsException:length=1;index=12感谢您的回复!我尝试了您的解决方案,但现在出现错误java.lang.ArrayIndexOutOfBoundsException:length=8;index=11我在答案中包含了另一个可能导致此问题的问题。删除“empty”后,仅此数组的值为8带有正则表达式的字符串。我试图检索的文件都在每个元素的名称下。因此“12”在内部存储器中有一个名为“12”的文件。是的,错误相同。出于某种原因,索引正在使用给定length=1和index=12的元素的实际值停止从数组中删除空的代码并再次运行
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();
}