Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infinite Scroll issue using EndlesScroll Android

Tags:

android

I am using the this for Pagination. But I have problem that it scrolls the page infinitely. For e.g. If my last page have products less than limit i.e. 4 products are loaded after 2 pages with 20 products each, then that 4 products start repeating endlessly. I am using following code to show products:

        grid.setOnScrollListener(new EndlessScrollListener() {
        @Override
        public boolean onLoadMore(int page, int totalItemsCount) {
            if (query != null || featureProductJson != null) {
                return empty;
            }

            if (!empty && !loading) {
                page++;
                productList product = new productList();
                product.execute(category_id, String.valueOf(limit), String.valueOf(page));
            }
            return empty;
        }
    });

My AsyncTask for product loading:

private class productList extends AsyncTask<String, Void, JSONObject> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        loading = true;
        showProgressDialog(cpv);
    }

    @Override
    protected JSONObject doInBackground(String... params) {
        return NetworkAdapter.getProduct(getActivity(), prefs.getAccessToken(), params[0], params[1], params[2]);
    }

    @Override
    protected void onPostExecute(JSONObject result) {
        super.onPostExecute(result);
        hideProgressDialog(cpv);
        loading = false;
        LogManager.e(TAG, "onPostExecute ============== :: " + result);

        try {
            if (result == null) {
                return;
            }

            if (result.has("statusCode")) {
                if (result.getInt("statusCode") == NetworkResponse.CONNECTION_ERROR) {
                    GeneralFunctions.showShortToast(getActivity(), getResources().getString(R.string.connection_error));
                    return;

                } else if (result.getInt("statusCode") == NetworkResponse.SYSTEM_ERROR) {
                    GeneralFunctions.showShortToast(getActivity(), result.getString("statusText"));
                    return;
                }
            }


            String data = result.toString();
            Object json = new JSONTokener(data).nextValue();
            if (json instanceof JSONObject) {

                //String obj = result.getString("success");
                boolean success = result.getBoolean("success");


                if (!success) {
                    empty  = true;
                } else {
                    JSONArray array = result.getJSONArray("data");
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject object = array.getJSONObject(i);
                        Product product = new Product();
                        product.setName(object.getString("name"));
                        product.setImage(object.getString("image"));
                        product.setDesc(object.getString("description"));
                        product.setPrice(object.getString("price"));
                        product.setProductId(object.getString("id"));
                        product.setSpecialPrice(object.getString("special"));
                        product.setQuantity(object.getString("quantity"));
                        productList.add(product);
                    }
                    adapter.notifyDataSetChanged();
                    page++;
                }
            }
            //you have an object
            else if (json instanceof JSONArray) {

            }
        } catch (JSONException e) {
            empty  = true;
            e.printStackTrace();
        }
    }
}

Please help what I am doing wrong.

Logcat:

    04-15 12:32:11.157 4630-4630/com.eCommerce E/ProductFragment: onPostExecute ============== :: {"data":[{"length_class":"cm","location":"","tag":"samsung, galaxy, sduos, white","price_formated":"Rs4,500","weight_class_id":"1","mpn":"","height":"0.00000000","attribute_groups":[],"description":"<div class=\"sidebar\" id=\"column-left\">\n<div class=\"box category highlights\">\n<div class=\"box-heading\"><span>SPECIFICATIONS<\/span><\/div>\n<\/div>\n<\/div>\n<div class=\"box productcarousel\">\n<div class=\"box-heading \"><span>GENERAL FEATURES<\/span><\/div>\n<\/div>\n\n<table>\t<tbody style=\"margin: 0px; padding: 0px;\">\n\t\t\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Brand<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Samsung<\/td>\n\t\t<\/tr>\n\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Form<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Bar<\/td>\n\t\t<\/tr>\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Call Features<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Loudspeaker<\/td>\n\t\t<\/tr>\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Model Name<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Galaxy S Duos 3 VE<\/td>\n\t\t<\/tr>\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Touch Screen<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Yes<\/td>\n\t\t<\/tr>\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">SIM Type<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Dual Sim, GSM + GSM<\/td>\n\t\t<\/tr>\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Model ID<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">SM-G316H<\/td>\n\t\t<\/tr>\n\t<\/tbody>\n<\/table>\n\n<div class=\"box productcarousel\"><div class=\"box-heading \"><span>MULTIMEDIA<\/span><\/div><\/div><table><tbody style=\"margin: 0px; padding: 0px;\">\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Video Player<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Yes<\/td>\n\t\t<\/tr>\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Music Player<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Yes, Supports MP3<\/td>\n\t\t<\/tr>\n\t<\/tbody>\n<\/table>\n\n<div class=\"box productcarousel\"><div class=\"box-heading \"><span>CAMERA<\/span><\/div><\/div><table><tbody style=\"margin: 0px; padding: 0px;\">\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Video Recording<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Yes, 720 x 1280<\/td>\n\t\t<\/tr>\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Flash<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">LED<\/td>\n\t\t<\/tr>\n\t\t<tr style=\"margin

04-15 12:32:11.247 4630-5268/com.eCommerce E/NetworkAdapter: SERVICE_URL ============== :: http://websiteaddresshere/index.php?route=feed/rest_api/products&category=92&limit=30&page=3

04-15 12:32:17.947 4630-5268/com.eCommerce E/NetworkAdapter: Categories response ============== :: {"success":true,"data":[{"id":"1254","seo_h1":"","name":"Samsung Galaxy Sduos 3 White","manufacturer":"Samsung","sku":"","model":"SM-G316 White","image":"http:\/\/websiteaddresshere\/image\/cache\/catalog\/samsung\/sduos3w3-500x500.jpg","images":["http:\/\/websiteaddresshere\/image\/cache\/catalog\/samsung\/sduos3w3-500x500.jpg","http:\/\/websiteaddresshere\/image\/cache\/catalog\/samsung\/sduos3w2-500x500.jpg","http:\/\/websiteaddresshere\/image\/cache\/catalog\/samsung\/sduos3side-500x500.jpg"],"price":"4,500","price_formated":"Rs4,500","rating":0,"description":"<div class=\"sidebar\" id=\"column-left\">\n<div class=\"box category highlights\">\n<div class=\"box-heading\"><span>SPECIFICATIONS<\/span><\/div>\n<\/div>\n<\/div>\n<div class=\"box productcarousel\">\n<div class=\"box-heading \"><span>GENERAL FEATURES<\/span><\/div>\n<\/div>\n\n<table>\t<tbody style=\"margin: 0px; padding: 0px;\">\n\t\t\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Brand<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Samsung<\/td>\n\t\t<\/tr>\n\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Form<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Bar<\/td>\n\t\t<\/tr>\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Call Features<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Loudspeaker<\/td>\n\t\t<\/tr>\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Model Name<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Galaxy S Duos 3 VE<\/td>\n\t\t<\/tr>\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Touch Screen<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Yes<\/td>\n\t\t<\/tr>\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">SIM Type<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Dual Sim, GSM + GSM<\/td>\n\t\t<\/tr>\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Model ID<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">SM-G316H<\/td>\n\t\t<\/tr>\n\t<\/tbody>\n<\/table>\n\n<div class=\"box productcarousel\"><div class=\"box-heading \"><span>MULTIMEDIA<\/span><\/div><\/div><table><tbody style=\"margin: 0px; padding: 0px;\">\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Video Player<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Yes<\/td>\n\t\t<\/tr>\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Music Player<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Yes, Supports MP3<\/td>\n\t\t<\/tr>\n\t<\/tbody>\n<\/table>\n\n<div class=\"box productcarousel\"><div class=\"box-heading \"><span>CAMERA<\/span><\/div><\/div><table><tbody style=\"margin: 0px; padding: 0px;\">\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Video Recording<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; paddi

04-15 12:32:17.987 4630-4630/com.eCommerce E/ProductFragment: onPostExecute ============== :: {"data":[{"length_class":"cm","location":"","tag":"samsung, galaxy, sduos, white","price_formated":"Rs4,500","weight_class_id":"1","mpn":"","height":"0.00000000","attribute_groups":[],"description":"<div class=\"sidebar\" id=\"column-left\">\n<div class=\"box category highlights\">\n<div class=\"box-heading\"><span>SPECIFICATIONS<\/span><\/div>\n<\/div>\n<\/div>\n<div class=\"box productcarousel\">\n<div class=\"box-heading \"><span>GENERAL FEATURES<\/span><\/div>\n<\/div>\n\n<table>\t<tbody style=\"margin: 0px; padding: 0px;\">\n\t\t\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Brand<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Samsung<\/td>\n\t\t<\/tr>\n\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Form<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Bar<\/td>\n\t\t<\/tr>\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Call Features<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Loudspeaker<\/td>\n\t\t<\/tr>\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Model Name<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Galaxy S Duos 3 VE<\/td>\n\t\t<\/tr>\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Touch Screen<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Yes<\/td>\n\t\t<\/tr>\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">SIM Type<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Dual Sim, GSM + GSM<\/td>\n\t\t<\/tr>\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Model ID<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">SM-G316H<\/td>\n\t\t<\/tr>\n\t<\/tbody>\n<\/table>\n\n<div class=\"box productcarousel\"><div class=\"box-heading \"><span>MULTIMEDIA<\/span><\/div><\/div><table><tbody style=\"margin: 0px; padding: 0px;\">\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Video Player<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Yes<\/td>\n\t\t<\/tr>\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Music Player<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Yes, Supports MP3<\/td>\n\t\t<\/tr>\n\t<\/tbody>\n<\/table>\n\n<div class=\"box productcarousel\"><div class=\"box-heading \"><span>CAMERA<\/span><\/div><\/div><table><tbody style=\"margin: 0px; padding: 0px;\">\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Video Recording<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Yes, 720 x 1280<\/td>\n\t\t<\/tr>\n\t\t<tr style=\"margin: 0px; padding: 0px;\">\n\t\t\t<td class=\"specsKey\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">Flash<\/td>\n\t\t\t<td class=\"specsValue\" style=\"margin: 15px 0px; padding: 12px; vertical-align: top;\">LED<\/td>\n\t\t<\/tr>\n\t\t<tr style=\"margin
like image 469
عثمان غني Avatar asked Apr 15 '16 01:04

عثمان غني


1 Answers

The problem from initial analysis of the code seems to be that you haven't written any code to handle the situation where the end of page is reached i.e there are not any more items to be loaded.

Another problem may be from the API end. From the logcat it seems you are receiving the same data even when the page number exceeds the available number of pages, I may be wrong you will have to verify it.For eg. there are 2 pages only and 2nd page has 4 items. What is the response when page number 3 is passed ? Is it same as page number 2 or empty ?

Device some kind of logic depending upon your requirements to find when the end of pages has reached and then stop executing the async task when it does.

Side note:

Please follow Java class naming conventions You are parsing the response in onPostExecute method by converting the datat to string and then again converting string to JSONTokener, which means you are effectively parsing the data in UI thread. Do the parsing in doInBackground method itself since that will happen in worker thread.

like image 111
Anirudha Agashe Avatar answered Sep 20 '22 18:09

Anirudha Agashe