无法使用AsyncTask加载图像

时间:2014-02-19 06:59:16

标签: android gridview android-asynctask

我有一个场景,我需要在加载主页时从URL检索图像。主页由30张图片组成,其中最初只加载了10张图片。并且在滚动时,还检索其他图像。(我不是指PullToRefresh功能)。我在StaggeredGridView(来自Github的库)中设置图像。我能够加载来自WebService调用的所有图像并能够在logcat中打印其名称,但无法设置除第一个以外的所有图像。并且在滚动时调用第二个asyn任务来加载接下来的10个图像图像,也只设置来自第二个异步任务的图像,这也是第一个图像。(这里参考第11个图像)。我已将notifyDataSetChanged调用到适配器,但它会抛出NullPointerException。

代码:

WebServicesClass:

doInBackGround()的内容如下:

               String image_url = "http://demo.bsetec.com/fancyclone/uploads/approved_items/"
                                        + first_image;
                                // System.out.println("The image url is " +
                                // image_url);
                                URL url = new URL(image_url);
                                URLConnection conn = url.openConnection();

                                if (!(conn instanceof HttpURLConnection))
                                    throw new IOException(
                                            "Not an HTTP connection");

                                try {

                                    HttpURLConnection httpConn = (HttpURLConnection) conn;
                                    httpConn.setAllowUserInteraction(false);
                                    httpConn.setInstanceFollowRedirects(true);
                                    httpConn.setRequestMethod("GET");
                                    httpConn.connect();

                                    response = httpConn.getResponseCode();
                                    if (response == HttpURLConnection.HTTP_OK) {

                                        in = httpConn.getInputStream();
                                        compressed_image = image;

                                        BitmapFactory.Options options = new BitmapFactory.Options();
                                        options.inPurgeable = true;
                                        options.inJustDecodeBounds = true;
                                        options.inSampleSize = 1;
                                        options.inJustDecodeBounds = false;
                                        image = BitmapFactory.decodeStream(in,
                                                null, options);

                                    }
                                } catch (Exception ex) {
                                    throw new IOException("Error connecting");
                                }


                                item = new RowItem(image, product_name, cost,
                                        product_id, dream_status,username);
                                rowItems.add(item);

其中item是holder类的对象,它有get,set方法和rowItems是List类型为holder类的对象。

MainActivty.java:

 mGridView = (StaggeredGridView) findViewById(R.id.grid_view);

        context = getApplicationContext();

        new WebServicesClass();



        mGridView.setOnScrollListener(this);

        mGridView.setOnItemClickListener(this);
    }

    @Override
    protected void onSaveInstanceState(final Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putStringArrayList(SAVED_DATA_KEY, mData);
    }

    @Override
    public void onScrollStateChanged(final AbsListView view, final int scrollState) {
        Log.d(TAG, "onScrollStateChanged:" + scrollState);
    }

    @Override
    public void onScroll(final AbsListView view, final int firstVisibleItem, final int visibleItemCount, final int totalItemCount) {
        Log.d(TAG, "onScroll firstVisibleItem:" + firstVisibleItem +
                            " visibleItemCount:" + visibleItemCount +
                            " totalItemCount:" + totalItemCount);
        // our handling
        if (!mHasRequestedMore) {

            System.out.println("Inside the requested more");
            int lastInScreen = firstVisibleItem + visibleItemCount;
          //  if (lastInScreen >= totalItemCount) {

                Log.d(TAG, "onScroll lastInScreen - so load more");
                mHasRequestedMore = true;
                new WebServicesClass().onLoadMoreItems();
                mAdapter.notifyDataSetChanged();
                mHasRequestedMore = false;
            //}
        }
    }

我很乐意帮助澄清任何问题。提前致谢。

0 个答案:

没有答案