0

Everything is working smoothly, but when I scroll to the bottom of the list view the list view refreshes with the next page, and doesn't append to the bottom of the list view. How can I have so the new data gets placed beneath the current data?

My Code

        @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();
    }
}
Eric Goncalves
  • 5,253
  • 4
  • 35
  • 59

2 Answers2

1

The problem come from here

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

You should keep the adapter somewhere, after loading new json data, try to append it to the existing adapter and request invalidate the list view

VinhNT
  • 1,091
  • 8
  • 13
  • How would I go about keeping the adapter somewhere else? – Eric Goncalves Mar 05 '14 at 04:36
  • 1
    some thing like this ListView lstView; ArrayList Target = new ArrayList(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); lstView = getListView(); lstView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); lstView.setTextFilterEnabled(true); Target.add("hi"); Target.add("hello"); ; ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_checked, Target); setListAdapter(adapter); Target.add("myname"); adapter.notifyDataSetChanged(); } } – VinhNT Mar 05 '14 at 04:44
  • I replaced a few lines of code in the "HandleBlogResponse" class, but now when i scroll to the bottom, i get a null pointer exception. any idea why? – Eric Goncalves Mar 05 '14 at 05:08
  • private adapter; loadListView() { initadapter() } dataincome() { pustdatatoadapter(adapter, data) adapter.notifyDataSetChanged(); } – VinhNT Mar 05 '14 at 06:08
0

After getting data from JSON response update your adapter.

 adapter.notifyDataSetChanged();
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Arvind Kanjariya
  • 2,089
  • 1
  • 18
  • 23