0

在我的应用程序中,我使用 pulltorefresh 和 ScrollListener 概念来显示 facebook 提要数据。正如我们在 Android facebook 应用程序中看到的那样。

这是拉动刷新的代码

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

            }
        });

我的滚动监听类在这里

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



      });

我遇到的问题是,当我使用异步类从 facebookapi 加载数据时,当我拉动列表进行刷新时,scrollListener、totalItemcount 值变为 2。即使我清除了 pullrefresh 中的所有列表,我也不明白它是如何变为 2 的.

重要的是,当我使用普通方法而不是这个 Async 类时,我根本没有遇到这个问题。

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

这是我的异步课程

     class doInBack extends AsyncTask<URL, Integer, Long>
    {


        @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>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<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();
            }

    }

    }

我的正常方法是

 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>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<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<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();
                    }

            }   
4

1 回答 1

0

我认为这里有几个因素。首先,我不确定您正在使用什么 Pull to refresh library 或 widget,但如果它包括页眉和页脚视图,您的计数可能会因页脚和页眉视图而减少。我认为这就是为什么你的滚动视图被 2 调用的原因。

我建议您在刷新操作完成之前清除您的适配器,这可能会使一切正常工作(因为不会为空列表调用滚动侦听器)。

于 2012-11-17T14:33:38.573 回答