Edward
Edward

Reputation: 139

On scroll listener calling asynctask twice thrice or some times more on scrolling once on listview

As i am loading data from web through an api as user scrolls to bottom in listview but When i scroll on listview for once it calls asynctask for many times in my activity which causes duplicate data in listview and in case of exception lots of dialog and toast on activity and when i scrolled to bottom and simply just touch my listview on scroll method get fires which calls asynctask again for many time so please tel me how to prevent this.

here is my code of onscroll method.

listView.setOnScrollListener(new OnScrollListener() {
//              boolean mIsScrollingUp;
//              int mLastFirstVisibleItem;
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {
                    // TODO Auto-generated method stub
                    if(scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL)
                        userscrolled=true;





                }

                @Override
                public void onScroll(AbsListView view, int firstVisibleItem,
                        int visibleItemCount, int totalItemCount) {
                    // TODO Auto-generated method stub
                    int lasinscren = firstVisibleItem + visibleItemCount;










                    if(userscrolled&&(lasinscren==totalItemCount)&&!lodinmore&&(visibleItemCount<totalItemCount)&&havedata)
                    {
                        progress = true;
                        if(Search_API==false){

                            if(connectionDetector.isConnectintoInternet()){
                                System.out.println("inside listview on scroll function");
                                    darList_task =  new DAR_list_task(DAR_Activity.this).execute();
                                        lodinmore = true;

                                }else
                            {

                                Toast.makeText(getApplicationContext(), "No internet connection", Toast.LENGTH_SHORT).show();



                            }
                            }else
                            {

                                new search_task(DAR_Activity.this).execute();

                            }

                    }

                }
            });

here's my asynctask

 public class DAR_list_task extends AsyncTask<Void, Void, Void>
{
      Context activity;

      public DAR_list_task(Context activity) {
        // TODO Auto-generated constructor stub

          this.activity = activity;
    }


    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        if(progress==true){

        if(limit==0)
        {
        dialog  = new Dialog(DAR_Activity.this);
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        LinearLayout linearLayout  = (LinearLayout)getLayoutInflater().inflate(R.layout.dialog_progress, null);
        dialog.setContentView(linearLayout);
        ProgressBar progressBar1  = (ProgressBar)findViewById(R.id.progress_dialog);
        dialog.setCancelable(false);
        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        dialog.setCanceledOnTouchOutside(false);

        dialog.show();  
        dialog.getWindow().setGravity(Gravity.CENTER);
        }else
        {

            progressBar.setVisibility(View.VISIBLE);
        }
        }
    }



    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        response=null;

        lodinmore = true;


        URL Url;
        try {



            byte[] data;

            data = login_id.getBytes("UTF-8");
            String login_base_64 = Base64.encode(data);
            data = gcm_id.getBytes("UTF-8");
            String gcm_base64= Base64.encode(data);

            if(url.contentEquals(""))
            {
            Url  = new URL(getResources().getString(R.string.dar_list_view));
            }else
            {

                Url = new URL("http://"+url+"/smart_oms/dar_app/view_dar.php");
            }
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("login_id", login_base_64));
            nameValuePairs.add(new BasicNameValuePair("gcm_id", gcm_base64));
            nameValuePairs.add(new BasicNameValuePair("limit", String.valueOf(limit)));

            HttpURLConnection httpURLConnection = (HttpURLConnection)Url.openConnection();

            httpURLConnection.setRequestMethod("POST");

            httpURLConnection.setUseCaches(false);
            httpURLConnection.setDoOutput(true);

            OutputStream  os = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            bufferedWriter.write(getQueryString(nameValuePairs));
            bufferedWriter.flush();
            bufferedWriter.close();
            os.close();

            httpURLConnection.connect();

            InputStream inputStream = httpURLConnection.getInputStream();


            response = BufferReaderMaker.readContentFromIS(inputStream);




        }catch( final UnknownHostException ex)
        {
        runOnUiThread(new Runnable() {
            public void run() {

                Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_SHORT).show();

            }
        });





        }catch (final ConnectTimeoutException e) {
            // TODO: handle exception
            runOnUiThread(new Runnable() {
                public void run() {

                    Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();

                }
            });



        }


        catch (Exception e) {
            // TODO: handle exception


        }





        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);


         Object json = null;
        try {

        if(response!=null)
        {

            if(progress==false)
            {
                dar_List_Items.clear();
                dar_Aadapter = new DAR_Aadapter(DAR_Activity.this, dar_List_Items);
                listView.setAdapter(dar_Aadapter);
                dar_Aadapter.notifyDataSetChanged();


            }


            try {
                json = new JSONTokener(response).nextValue();
            } catch (JSONException e1) {
                // TODO Auto-generated catch block
                ErrorDialog errorDialog = new ErrorDialog();
                errorDialog.Dialog(activity, statusCode, response, login_id, "", new DAR_list_task(activity));
            }

                if(json instanceof JSONArray)
                {
                    havedata=false;
                    Toast.makeText(getApplicationContext(), "No More DAR", 0).show();

                }else if(json instanceof JSONObject)
                {





                    JSONObject jsonObject = new JSONObject(response);

                    JSONArray jsonArray = jsonObject.getJSONArray("view_dar_detail");

                    for(int i =0 ;i<jsonArray.length();)
                    {



                    JSONObject json_data = jsonArray.getJSONObject(i);
                    String dar_id = json_data.optString("customer_id"); 
                    String Customer_name = json_data.optString("customer_name");
                    String Contacted= json_data.optString("contact_person_name");
                    String product = json_data.optString("product");
                    String status = json_data.optString("dar_status");
                    String Contact_type = json_data.optString("contact_type");
                    String created_date = json_data.optString("dar_created_date");
                    String request_date = json_data.optString("requeste_date");



                    DAR_List_Item dar_List_Item  = new DAR_List_Item(dar_id,Customer_name, Contacted, Contact_type, product, status, created_date, request_date,"abcd");

                    dar_List_Items.add(dar_List_Item);




                    i++;
                    }



                    int index = listView.getFirstVisiblePosition();
                    View v= listView.getChildAt(0);
                    int top = (v == null) ? 0 : v.getTop();
                    listView.setAdapter(null);

                    DAR_Aadapter aadapter  = new DAR_Aadapter(DAR_Activity.this, dar_List_Items);
                    listView.setAdapter(aadapter);
                    aadapter.notifyDataSetChanged();
                    listView.setSelectionFromTop(index, top);


                }else
                {
                    ErrorDialog errorDialog = new ErrorDialog();
                    errorDialog.Dialog(activity, statusCode, response, login_id, "", new DAR_list_task(activity));



                }





                lodinmore = false;
            }

        lodinmore = false;


            } catch (JSONException e) {
                // TODO Auto-generated catch block
                ErrorDialog errorDialog = new ErrorDialog();
                errorDialog.Dialog(activity, statusCode, response, login_id, "", new DAR_list_task(activity));

        }
        if(progress==true){
            if(limit==0)
            {
                dialog.dismiss();
            }else{
            progressBar.setVisibility(View.GONE);
            }
            }
        if(json  instanceof JSONObject)
        limit = limit +10;



    }





}

pLease help me in this i have tried a lot of things like using flag value and many more but non of them working for me.

How to prevent calling asynctask calling many time? how to prevent asynctask to fired when i scrolled to bottom and just touch the list?

Upvotes: 0

Views: 2664

Answers (2)

Duy Nguyen
Duy Nguyen

Reputation: 11

An easy way is define a boolean param:

if(view.getLastVisiblePosition() == totalItemCount-1 && isLoadMore){
                isLoadMore = false;
                pageNum++;
                loadMoreData(searchStr, donViID, pageNum, Constant.RECORD_PER_PAGE);
            }

Upvotes: 1

Seshu Vinay
Seshu Vinay

Reputation: 13588

There is a work around for this:

    final int lastItem = firstVisibleItem + visibleItemCount;
                   if(lastItem == totalItemCount) {
                      if(lastLastitem !=lastItem){ //to avoid multiple calls for last item, declare it as a field in your Activity
                        lastLastitem = lastItem;
                        // Your async task here
                      }

Upvotes: 2

Related Questions