一切工作顺利,但是当我滚动到列表视图的底部时,列表视图会随下一页刷新,并且不会附加到列表视图的底部。我怎样才能将新数据放在当前数据下方?
我的代码
@Override
public void onScrollStateChanged(AbsListView listView, int scrollState) {
if (scrollState == SCROLL_STATE_IDLE) {
if (listView.getLastVisiblePosition() >= listView.getCount() - 1 - 1) {
CURRENT_PAGE_NUMBER = CURRENT_PAGE_NUMBER + 25;
Toast.makeText(getActivity(), CURRENT_PAGE_NUMBER + " - " + JSON_AFTER, Toast.LENGTH_SHORT).show();
GetBlogPost getBlogPost = new GetBlogPost();
getBlogPost.execute();
adapter.notifyDataSetChanged();
}
}
}
private class GetBlogPost extends AsyncTask<Object, Void, JSONObject> {
@Override
protected JSONObject doInBackground(Object... params) {
int responseCode;
JSONObject jsonResponse = null;
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.website.com/" + SUBS[Integer.parseInt(SUB)] + "/.json?count=" + CURRENT_PAGE_NUMBER + "&after=" + JSON_AFTER );
try {
HttpResponse response = client.execute(httpget);
StatusLine statusLine = response.getStatusLine();
responseCode = statusLine.getStatusCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while((line = reader.readLine()) != null){
builder.append(line);
}
jsonResponse = new JSONObject(builder.toString());
}
else {
Log.i(TAG, String.format("Unsuccessful HTTP response code: %d", responseCode));
}
}
catch (JSONException e) {
Log.v(TAG, "Exception caught!", e);
}
catch (Exception e) {
Log.v(TAG, "Exception caught!", e);
}
return jsonResponse;
}
@Override
protected void onPostExecute(JSONObject result) {
mBlogData = result;
handleAsyncResponse();
}
}
private void handleAsyncResponse() {
if (mBlogData != null) {
try {
JSONObject jsonData = mBlogData.getJSONObject("data");
JSONArray jsonChildren = jsonData.getJSONArray("children");
mPostTitles = new String[jsonChildren.length()];
for (int i = 0; i < jsonChildren.length(); i ++) {
JSONObject children = jsonChildren.getJSONObject(i);
JSONObject post = children.getJSONObject("data");
JSON_AFTER = jsonData.getString("after");
String title = post.getString("title");
title = Html.fromHtml(title).toString();
mPostTitles[i] = title;
Target.add(title);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, Target);
setListAdapter(adapter);
adapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1,
Target
);
setListAdapter(adapter);
}
} catch (JSONException e) {
Log.wtf(TAG, "Exception caught!", e);
}
} else {
Toast.makeText(
getActivity(),
"Nothing to display!",
Toast.LENGTH_SHORT).show();
}
}