0

我有一个简单的异步任务,如下面的片段

protected class DownloadArticlesAsyncTask extends AsyncTask<Category, Integer, HashMap<String, Object>> 
{
..
    @Override
    protected HashMap<String, Object> doInBackground(Category... categories) 
    {
        HashMap<String, Object> result = new HashMap<String, Object>();

        List<Article> articles = Util.getArticlesFromServer(categories);
        result.put("articles", articles);
        Log.d(TAG, "size = " + articles.size()); // Return 1

        return result;
    }

    @Override
    protected void onPostExecute(HashMap<String, Object> result) 
    {
        ArrayList<Article> articles = (ArrayList<Article>) result.get("articles");
        Log.d(TAG, "size = " + articles.size()); // Return 0, why?           
    }

}

为什么转移onPostExecute返回空的大小,它只是有时发生,而不是总是发生。

任何想法?

4

1 回答 1

0

尝试List<Articles> articles;在方法 doInBackground 之外声明...并更改List<Article> articles = Util.getArticlesFromServer(categories);articles = Util.getArticlesFromServer(categories);

它返回零,因为数据保存在未声明为全局的 List 中。

于 2013-08-18T10:14:16.127 回答