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 + "¶m2=" + param2 + "¶m3=" + 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!