0

I have a ListView which reading data from JSON Adapter I created. Since my API is limited to 10 results per page, I want to show more results when the user reaches the end of the listview.

This is what I created so far:

The following code is creating the list and check if the user reached to the end of the list. This code located in my Fragment.

mainListView = (StickyListHeadersListView) v.findViewById(R.id.main_listview);
        mJSONAdapter = new JSONAdapter(getActivity(), inflater);
        mainListView.setAdapter(mJSONAdapter);

        mainListView.setOnScrollListener(new AbsListView.OnScrollListener() {

            public void onScrollStateChanged(AbsListView view, int scrollState) {


            }

// Check if we reached the end of the list

            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {

                if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount != 0) {
                    searchDistance = sharedPrefs.getDistance(getActivity());
                    if (flag_loading == false) {
                        flag_loading = true;
                        query(param1, param2, param3, **PAGE NUMBER**);
                    }
                }
            }
        });

And this is my query() Method:

private void query(double param1, double param2, int param3, int page) {

        // Create a client to perform networking
        AsyncHttpClient client = new AsyncHttpClient();

        // Show ProgressDialog to inform user that a task in the background is occurring
        mProgress.setVisibility(View.VISIBLE);

        // Have the client get a JSONArray of data
        // and define how to respond

        client.get(MY_API_URL + "?param1=" + param1 + "&param2=" + param2 + "&param3=" + param3 + "&page=" + page,
                new JsonHttpResponseHandler() {

                    @Override
                    public void onSuccess(JSONObject jsonObject) {

                        // 11. Dismiss the ProgressDialog
                        mProgress.setVisibility(View.GONE);

                        // update the data in your custom method.
                        mJSONAdapter.updateData(jsonObject.optJSONArray("docs"));
                    }

                    @Override
                    public void onFailure(int statusCode, Throwable throwable, JSONObject error) {

                        // 11. Dismiss the ProgressDialog
                        mProgress.setVisibility(View.GONE);

                        getActivity().setContentView(R.layout.bad_connection);
                    }
                });
    }

mJSONAdapter.updateData():

public void updateData(JSONArray jsonArray) {
        // update the adapter's dataset
        mJsonArray = jsonArray;
        notifyDataSetChanged();
    }

This actual code update the whole list and removing the old data. what I want to do is ADD the new data when the user scrolled to the end.

Hope you can help me, Thanks!

Eliran
  • 207
  • 1
  • 2
  • 11

2 Answers2

0

In your code you're over-writing your data rather than adding.

Create an ArrayList of the objects you receive from your json file. For instance:

// as a global variable in your Fragment
ArrayList<String> data = new ArrayList<>(); 

.. then initialize your JSONAdapter this way

mJSONAdapter = new JSONAdapter(getActivity(), inflater, data);

that is, supposing you create a constructor for your JSONAdapter as follows

public JSONAdapter(Context c, LayoutInflater i, ArrayList<String> data) {
     super(context, 0, data);
 }

... then in your query() method under the JsonHttpResponseHandler().onSuccess() do this:

client.get(MY_API_URL + "?param1=" + param1 + "&param2=" + param2 + "&param3=" + param3 + "&page=" + page,
            new JsonHttpResponseHandler() {

                @Override
                public void onSuccess(JSONObject jsonObject) {

                    // 11. Dismiss the ProgressDialog
                    mProgress.setVisibility(View.GONE);

                    // update the data in your custom method.
                    //mJSONAdapter.updateData(jsonObject.optJSONArray("docs"));
                    JSONArray resultArray = jsonObject.optJSONArray("docs");
                    for (int i=0; i<resultArray.length(); i++) {
                        // the global field data gets updated rather than over-written
                       data.add(resultArray.get(i));
                    }
                    mJSONAdapter.update();
                }
        // ...... rest of code is probably fine
}

also change your JSONAdapter update() method to something like this

public void update() {
    notifyDataSetChanged();
}
De-Great Yartey
  • 1,574
  • 2
  • 12
  • 16
0

you can download this lib from github and use it. https://github.com/nicolasjafelle/PagingListView. it work for me perfectly, i hope that will help u ;)

Ahmed.ess
  • 265
  • 3
  • 10