Asynctask with pulltorefresh listview in android
<p>In my application I am using pulltorefresh and ScrollListener concepts to show facebook feed data.As we can see in Android facebook app. </p>

<p>Here is code for pull to refresh</p>

<pre><code>listView.setOnRefreshListener(new OnRefreshListener() {


            public void onRefresh() {
                // Your code to refresh the list contents goes here
                scroll=true;
                pic.clear();
                id.clear();
                name.clear();
                msg.clear();
                img.clear();
                profimg.clear();
                objid.clear();
                comment.clear();
                 previousTotal = 0;
                  loading = true;               
                listView.setAdapter(null);


                j=0;
                webserv="https://graph.facebook.com/me/home?access_token="+accesstoken;
                 doInBack dob=new doInBack();
                 dob.execute();  
//               doback(webserv);
                    Log.e("hi","doback called");

                // Make sure you call listView.onRefreshComplete()
                // when the loading is done. This can be done from here or any
                // other place, like on a broadcast receive from your loading
                // service or the onPostExecute of your AsyncTask.

                // For the sake of this sample, the code will pause here to
                // force a delay when invoking the refresh

            }
        });
</code></pre>

<p>And my scrolllistener class is here</p>

<pre><code>    listView.setOnScrollListener(new  OnScrollListener() {

            private int threshold = 0;
//          
            public void onScrollStateChanged(AbsListView view, int scrollState) {
//                  
                  if (scrollState != 0) {  
                    isScrolling = true; 
                }
                else {   
                   isScrolling = false;  
                  adapter.notifyDataSetChanged();


                }  

            }


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

//           public void onScrollStateChanged(AbsListView view , int scrollState)
//           {
//                  if (scrollState != 0) {  
//                      listView.getAdapter().isScrolling = true; 
//                  }
//                  else {   
//                      listView.getAdapter().isScrolling = false;  
//                      listView.getAdapter().notifyDataSetChanged();  
//                  }  
//              }  
              // TODO Auto-generated method stub

              if (loading) {
                  if (totalItemCount &gt; previousTotal) {
                      System.out.println(totalItemCount);
                      loading = false;
                      previousTotal = totalItemCount;
                  }
              } 
//                   int lastInScreen = firstVisibleItem + visibleItemCount; 
//              if (!loading &amp;&amp; (totalItemCount - visibleItemCount) ==(firstVisibleItem + threshold)){
                   if (!loading &amp;&amp; (firstVisibleItem + visibleItemCount) &gt;= totalItemCount){
//                    if(((firstVisibleItem + visibleItemCount) == totalItemCount)){
//                   if (!(loading)  &amp;&amp;(totalItemCount - visibleItemCount) ==(firstVisibleItem + threshold)) {
//                clearAllResources(); 
                  scroll=false;
//                  if (!(loading)  &amp;&amp;(totalItemCount - visibleItemCount) == (firstVisibleItem + threshold)) {
                       Log.v("in gridview loading more","grid load");
//                      
                       doInBack dob=new doInBack();
                       dob.execute();  
//                       doback(webserv);
                    loading = true;
                   }
              }



      });
</code></pre>

<p>The issue i am getting is when I use async class for loading data
from facebookapi ,the scrollListener, totalItemcount value is becoming
2 when I pull my list to refresh.I didnt understand   how it is
becoming 2 eventhough I clear all the list in my pullrefresh.</p>

<p>The important thing is I am not getting this issue at all when I use
normal method instead of this Async class.</p>

<pre><code> Here I am providing both my doback() and async class,I think there is something going wrong when I use Async class.
</code></pre>

<p>Here is my Async class</p>

<pre><code>     class doInBack extends AsyncTask&lt;URL, Integer, Long&gt;
    {


        @Override
        protected Long doInBackground(URL... arg0) {


//          dialog=MyProgressDialog.show(Wall.this, null,null);

            Log.e("hi","doback parsing");
//          int count=0;

            currentweb= webserv;
            try
            {   
//              if(urlval&gt;0){
                    wallres=UrltoValue.getValuefromUrl(webserv);
                    Log.e("wallrespages",wallres);
//              }
//              else{
//              wallres=UrltoValue.getValuefromUrl(web+accesstoken);
//              Log.e("wallreshome",wallres);
//              }
                JSONObject jobj1=new JSONObject(wallres);

                JSONObject jobj2=jobj1.getJSONObject("paging");
                webserv= jobj2.getString( "next");

                 jsonArray = jobj1.getJSONArray("data");




                 for(int i=0;i&lt;jsonArray.length();i++){

                     jsonObject = jsonArray.getJSONObject(i);





                    if(jsonObject.has("message")||jsonObject.has("picture")) {

                        try{


//                        msg[j]=jsonObject.getString("message");
                            if(jsonObject.has("message"))
                            {

                                msg.add(jsonObject.getString("message"));   
                            }
                            else{
                                msg.add("");    
                            }
                        }
                        catch(Exception e){
                            e.printStackTrace();
                        }

                        try{

//                            msg[j]=jsonObject.getString("message");
                                if(jsonObject.has("picture"))
                                {
                                    String firstpicture=jsonObject.getString("picture");
                                    String secondpicture=firstpicture.replaceAll("_s.jpg", "_n.jpg");
                                     Log.e("picurl",secondpicture);
                                     pic.add(secondpicture);

                                }
                                else{
                                    pic.add("");
                                }
                            }
                            catch(Exception e){
                                e.printStackTrace();
                            }

                        objid.add(jsonObject.getString("id"));

                          JSONObject jobj=jsonObject.getJSONObject("from");
                           name.add(jobj.getString("name"));
                           id.add(jobj.getString("id"));
//                         Log.e("msg",msg);
//                            
//                         Log.e("name",name[j]);
//                         Log.e("id",id[j]);
                           profimg.add("http://graph.facebook.com/"+id.get(j)+"/picture?type=square");

                           JSONObject commentjobj=jsonObject.getJSONObject("comments");
                            comment.add(commentjobj.getString("count"));
                            weburl.add( currentweb);

                           Log.e("comment", comment.get(j));
//                         bitmap[j]= getBitmapFromURL(profimg[j]);
//                         profimg1.setImageBitmap(bitmap[j]);
//                         imageLoader.DisplayImage( profimg[j].replace(" ", "%20"), profimg1) ;
//                         System.out.println( bitmap[j]);
                           j++;
//                          f=j;
                    }
                    }


//                if(jsonObject.getString("message")!=""){
//                    msg[i]=jsonObject.getString("message");
//                     
//                    JSONObject jobj=jsonObject.getJSONObject("from");
//                     name[i]= jobj.getString("name");
//                     id[i]=jobj.getString("id");
//                     Log.e("msg",msg[i]);
//                     Log.e("name",name[i]);
//                     Log.e("id",id[i]);
//                     }
//                       
//                   }





            }

            catch(Exception e)
            {
                e.printStackTrace();
            }

            return null;
        }

        protected void onPostExecute(Long result) { 

            try
            {  
                System.out.println(scroll);
                if(scroll){
//                   listView.addFooterView(footerView);
                 adapter=new MySimpleArrayAdapter(Wall.this,R.layout.wall,pic,name,msg,id,profimg,bitmap,comment,objid,weburl);
                listView.setAdapter(adapter);

//              dialog.dismiss();
                listView.postDelayed(new Runnable() {

//                  @Override
                    public void run() {

                        listView.onRefreshComplete();
                    }
                }, 2000);

                }


         else{
//           adapter=new MySimpleArrayAdapter(Wall.this,R.layout.wall,pic,name,msg,id,profimg,bitmap,comment,objid,weburl);
             adapter.notifyDataSetChanged();
             listView.postDelayed(new Runnable() {

//                  @Override
                    public void run() {

                        listView.onRefreshComplete();
                    }
                }, 2000);

            }
            }

            catch(Exception e)
            {
                e.printStackTrace();
//              dialog.dismiss();
            }

    }

    }
</code></pre>

<p>And my normal method is</p>

<pre><code> public void doback(String url) 
        {

            currentweb=url;


//              dialog=MyProgressDialog.show(Wall.this, null,null);

                Log.e("hi","doback parsing");
//              int count=0;

                try
                {   
//                  if(urlval&gt;0){
                        wallres=UrltoValue.getValuefromUrl(currentweb);
                        Log.e("wallrespages",wallres);
//                  }
//                  else{
//                  wallres=UrltoValue.getValuefromUrl(web+accesstoken);
//                  Log.e("wallreshome",wallres);
//                  }
                    JSONObject jobj1=new JSONObject(wallres);

                    JSONObject jobj2=jobj1.getJSONObject("paging");
                    webserv= jobj2.getString( "next");

                     jsonArray = jobj1.getJSONArray("data");
//                   
//                    for(int k=0;k&lt;jsonArray.length();k++){
//                       
//                       jsonObject = jsonArray.getJSONObject(k);
//                       if(jsonObject.has("message")||jsonObject.has("picture")) {
//                              count++;
//                       }
//                    }
//                    jsonObject=null;

//                   msg=new String[count];
//                   name=new String[count];
//                   id=new String[count];
//                   profimg=new String[count];
//                   bitmap=new Bitmap[count];
//                   pic=new String[count];

                     for(int i=0;i&lt;jsonArray.length();i++){

                         jsonObject = jsonArray.getJSONObject(i);





                        if(jsonObject.has("message")||jsonObject.has("picture")) {

                            try{


//                            msg[j]=jsonObject.getString("message");
                                if(jsonObject.has("message"))
                                {

                                    msg.add(jsonObject.getString("message"));   
                                }
                                else{
                                    msg.add("");    
                                }
                            }
                            catch(Exception e){
                                e.printStackTrace();
                            }

                            try{

//                                msg[j]=jsonObject.getString("message");
                                    if(jsonObject.has("picture"))
                                    {
                                        String firstpicture=jsonObject.getString("picture");
                                        String secondpicture=firstpicture.replaceAll("_s.jpg", "_n.jpg");
                                         Log.e("picurl",secondpicture);
                                         pic.add(secondpicture);

                                    }
                                    else{
                                        pic.add("");
                                    }
                                }
                                catch(Exception e){
                                    e.printStackTrace();
                                }

                            objid.add(jsonObject.getString("id"));

                              JSONObject jobj=jsonObject.getJSONObject("from");
                               name.add(jobj.getString("name"));
                               id.add(jobj.getString("id"));
//                             Log.e("msg",msg);
//                                
//                             Log.e("name",name[j]);
//                             Log.e("id",id[j]);
                               profimg.add("http://graph.facebook.com/"+id.get(j)+"/picture?type=square");

                               JSONObject commentjobj=jsonObject.getJSONObject("comments");
                                comment.add(commentjobj.getString("count"));
                                weburl.add(url);

                               Log.e("comment", comment.get(j));
//                             bitmap[j]= getBitmapFromURL(profimg[j]);
//                             profimg1.setImageBitmap(bitmap[j]);
//                             imageLoader.DisplayImage( profimg[j].replace(" ", "%20"), profimg1) ;
//                             System.out.println( bitmap[j]);
                               j++;
//                              f=j;
                        }
                        }

//                    if(jsonObject.getString("message")!=""){
//                        msg[i]=jsonObject.getString("message");
//                         
//                        JSONObject jobj=jsonObject.getJSONObject("from");
//                         name[i]= jobj.getString("name");
//                         id[i]=jobj.getString("id");
//                         Log.e("msg",msg[i]);
//                         Log.e("name",name[i]);
//                         Log.e("id",id[i]);
//                         }
//                           
//                       }





                }

                catch(Exception e)
                {
                    e.printStackTrace();
                }





                    try
                    {  
                        System.out.println(scroll);
                        if(scroll){

                         adapter=new MySimpleArrayAdapter(Wall.this,R.layout.wall,pic,name,msg,id,profimg,bitmap,comment,objid,weburl);
                        listView.setAdapter(adapter);
                          listView.postDelayed(new Runnable() {

//                              @Override
                                public void run() {

                                    listView.onRefreshComplete();
                                }
                            }, 2000);

                        dialog.dismiss();
                        }

                 else{
//                   adapter=new MySimpleArrayAdapter(Wall.this,R.layout.wall,pic,name,msg,id,profimg,bitmap,comment,objid,weburl);
                     adapter.notifyDataSetChanged();
                      listView.postDelayed(new Runnable() {

//                          @Override
                            public void run() {

                                listView.onRefreshComplete();
                            }
                        }, 2000);
                    }


                    }



                    catch(Exception e)
                    {
                        e.printStackTrace();
//                      dialog.dismiss();
                    }

            }   
</code></pre>
