我有一个从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");
}
}
答案 0 :(得分:1)
((GridView) list).setAdapter(adapter);
应该在您onCreate()
的活动中,而不是onPostExecute()
。
刷新listview中的数据。在images Arraylist
adapter.notifyDataSetChanged()
onPostExecute()
中致电AsyncTask
之前,您必须先修改{{1}}