1

我有一个从 URL 获取的图像网格视图,当活动开始时我加载了 24 个图像。现在当用户在底部滚动时,我需要加载例如。还有 8 张图片,但是当我这样做时,我的活动正在重新加载,并且没有加载新图片。有人可以帮我解决这个问题。

这是我的代码:

//load more images onclick
loadmore.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                start += 8;
                Log.v("--", start + " start");
                new getImagesTask().execute();
                adapter.notifyDataSetChanged();
                Log.v("--", images.size() + " G O L E M I N A !");
            }
        });

//and my asyncTask
public class getImagesTask extends AsyncTask<Void, Void, Void> {
        JSONObject json;
        ProgressDialog dialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = ProgressDialog.show(Main.this, "",
                    getString(R.string.loading_));
        }

        @Override
        protected Void doInBackground(Void... params) {

            URL url;
            if (start < totalItemCount) {
                //
                // while (start < totalItemCount) {

                try {
                    Log.v("--", start + "");
                    url = new URL(jsonURL + initialKeyWord + parameters + start);
                    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);
                    }

                    json = new JSONObject(builder.toString());
                    JSONObject responseObject = json
                            .getJSONObject("responseData");
                    JSONArray resultArray = responseObject
                            .getJSONArray("results");
                    int golemina = resultArray.length();
                    for (int k = 0; k < golemina; k++) {
                        images.add(getImageList(resultArray).get(k));
                    }

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                start += 8;

                this.doInBackground();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            ((GridView) list).setAdapter(adapter);
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
            adapter.notifyDataSetChanged();
            Log.v("--", images.size() + " g o l e m i n a");

        }
    }
4

1 回答 1

1

((GridView) list).setAdapter(adapter);应该进入onCreate()你的活动而不是onPostExecute()。刷新列表视图中的数据。您必须在images Arraylist调用之前修改您adapter.notifyDataSetChanged()onPostExecute()AsyncTask

于 2013-07-29T06:53:04.247 回答