【问题标题】:load data into an array using for loop android使用for循环android将数据加载到数组中
【发布时间】:2012-11-16 13:23:30
【问题描述】:

我正在尝试从 Post 类填充图像 url 数组,但该活动返回一个空白页面。我做得对吗?对不起,我学android不太懂。

ArrayList PostDetails = new ArrayList();

    for (int index=0;index<imageUrls.length;index++){
        imageUrls[index]=PostDetails.get(index).getImag();
    }

【问题讨论】:

    标签: android arrays for-loop image-caching


    【解决方案1】:

    你应该等到任务完成,你可以把最后一部分放在onPostExecute方法中..

    protected void onPostExecute( ) {
        String[]imageUrls= new String[PostDetails.size()];
    
       for (int index=0;index<imageUrls.length;index++){
           imageUrls[index]=PostDetails.get(index).getImag();
       }
    }
    

    【讨论】:

      【解决方案2】:

      Nunu 的回答是正确的,但我想稍微扩展一下。假设loadingTaskAsyncTask,因此当您调用execute 时,doInBackground 中的代码将异步执行。创建和填充数组的代码将与PostDetails 的填充并行执行,我假设它填充在loadingTask 中。由于数组循环将执行得非常快,它会在loadingTask 执行的网络操作之前​​很久就完成了。因此,您的页面是空白的,因为您在填充 PostDetails 之前已经遍历了数组。

      解决方案正如 Nunu 建议的那样,将 imageUrls 数组的种群移动到 AsyncTaskonPostExecute 方法中。此外,任何使用此数组的 UI 操作也应移至 onPostExecute

      【讨论】:

        猜你喜欢
        • 2017-10-02
        • 1970-01-01
        • 1970-01-01
        • 2020-10-22
        • 1970-01-01
        • 1970-01-01
        • 2019-04-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多