0

一切工作顺利,但是当我滚动到列表视图的底部时,列表视图会随下​​一页刷新,并且不会附加到列表视图的底部。我怎样才能将新数据放在当前数据下方?

我的代码

        @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();
    }
}
4

2 回答 2

1

问题来自这里

adapter = new ArrayAdapter<String>...
setListAdapter(adapter);

您应该将适配器保存在某处,在加载新的 json 数据后,尝试将其附加到现有适配器并请求使列表视图无效

于 2014-03-05T04:35:43.653 回答
0

尝试以下链接以更新滚动方法的列表视图。

滚动列表视图

从 JSON 响应获取数据后,更新您的适配器。

 adapter.notifyDataSetChanged();
于 2014-03-05T04:41:11.233 回答