I debugged several times. It's driving me crazy. My listView.onScroll() function gets called several times and every time with arguments all 0! Which causes the listView to show THE SAME list over and over when scrolled.
I even manually set the arguments but still no change.
Why on earth would onScroll get called multiple times?
It's not like it gets caught in a loop because some times the result appears on the screen!
I call a RESTfull webService wich has pagination. Here is a piece of my code in my listView Fragment. Please if any other part of it is needed leave me a comment and I will add.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_search_result_list, container, false);
list=(ListView)rootView.findViewById(R.id.listViewSearchResult);
list.setOnItemClickListener((OnItemClickListener) getActivity());
list.setOnScrollListener(new OnScrollListener(){
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
pagination = new Pagination(firstVisibleItem+visibleItemCount,visibleItemCount);
int lastInScreen = firstVisibleItem + visibleItemCount;
if((lastInScreen == totalItemCount) && !(loadingMore)){
//grabURL(url);
new JSONParse(getActivity()).execute(url);
}
if (firstVisibleItem==0)
totalItemCount = 5;
}
});
return rootView;
}
//*********************************** inner class
public class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
private SearchResultArrayListAdaptor adapter;
Context mContext;
int checkBoxRooms;
public JSONParse(Context context){
mContext = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected JSONObject doInBackground(String... args) {
//vvvv
loadingMore = true;
JSONObject json = null;
PropertyFilter searchFilter = SearchFilterManager.initializePropertyFilter(new PropertyFilter(), getArguments());
JSONParser jParser = new JSONParser();
if (pagination.getPageSize()==0)
pagination.setPageSize(5);
json = jParser.getJSONFromUrl(url, searchFilter, pagination);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
loadingMore = false;
PropertyObject propertyObject;
ArrayList<PropertyObject> arrayList = new ArrayList<PropertyObject>();
try {
jsonArray = json.getJSONArray("PropertyListings");
if (jsonArray == null || jsonArray.length()<1)
return;
for(int i = 0; i < jsonArray.length(); i++){
JSONObject c = jsonArray.getJSONObject(i);
propertyObject = new Gson().fromJson(c.toString(), new PropertyObject().getClass());
arrayList.add(propertyObject);
}
adapter = new SearchResultArrayListAdaptor(getActivity(), R.layout.list_view_items, arrayList);
list.setAdapter(adapter);
}
catch (JSONException e) {
e.printStackTrace();
}
From the developer's site about AbsListView.OnScrollListener:
The callback will be called in every frame. So it is not a good idea to use this method.
Alternately, you can implement a custom adapter in your listview and inside its getView() method check if the position variable point's to the last item on the list. If yes make the request.