Loader没有调用loadInBackground方法

时间:2015-02-14 15:11:40

标签: android

所以我有一个加载器,它创建了精细甚至调用构造函数,但不是loadInBackground方法

在片段i中使用它初始化加载器:

getLoaderManager().initLoader(SearchHelper.JSON_LOADER_ID, 
                null, new JsonLoaderHandler());

JsonLoaderHandler:

private class JsonLoaderHandler implements LoaderCallbacks<ArrayList<ImageItem>> {

        @Override
        public Loader<ArrayList<ImageItem>> onCreateLoader(int id, Bundle args) {
            JsonLoader loader = new JsonLoader(getActivity(), URL, SearchFragment.this);
            return loader;
        }

        @Override
        public void onLoadFinished(Loader<ArrayList<ImageItem>> loader, ArrayList<ImageItem> data) {
            ImageAdapter adapter = new ImageAdapter(data, getActivity());
            pager.setAdapter(adapter);
        }

        @Override
        public void onLoaderReset(Loader<ArrayList<ImageItem>> arg0) {
            //NO-OP
        }


    }

OnCreateLoader也在调用和构造函数,但loadInBackground不是:

private String URL;
private ISearchProcess process;

public JsonLoader(Context context, String URL, ISearchProcess process) {
    super(context);

    this.URL = URL;
    this.process = process;
}

@Override
public ArrayList<ImageItem> loadInBackground() {
    process.showProgressDialog();
    URL url;
    try {
        url = new URL(URL);

        URLConnection connection = url.openConnection();

        String line;
        StringBuilder builder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        while ((line = reader.readLine()) != null) {
            builder.append(line);
        }

        JSONObject json = new JSONObject(builder.toString());
        JSONObject responseObject = json.getJSONObject("responseData");
        JSONArray resultArray = responseObject.getJSONArray("results");
        return getImageItems(resultArray);
    } catch (Exception e) {
        if (e instanceof NetworkErrorException) {
            stopLoading();
            process.showNoInternetDialog();
        }
        e.printStackTrace();
    }

    return null;
}

请告诉我这里做错了什么

1 个答案:

答案 0 :(得分:0)

尝试添加 forceLoad()

getLoaderManager().initLoader(SearchHelper.JSON_LOADER_ID, 
            null, new JsonLoaderHandler()).forceLoad();