AsyncTask正在向listview添加额外的项目

时间:2012-11-26 09:32:26

标签: android

我在我的应用程序中使用了一个pulltorefresh listview。当我调用pulltoRefresh时,我调用了AsyncTask.Before调用它之前我清除了ArrayLists中的所有数据并将我的适配器设置为null。 以下代码显示了此

     listView.setOnRefreshListener(new OnRefreshListener() {

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

                  listView.removeFooterView(footerView);
//              listView.setAdapter(null);
//              scroll=true;
//              notifyDataSetChanged();
//              urlval=0;
                j=0;
                 loading = true;
                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

            }
        });

当我调用这个Asyntask时,会立即添加一个我们无法看到的项目。我在onScrollListener类中观察到了这一点,我用它来加载更多数据。

我的onScrollListener类是

     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) {
                    Log.e("in loading","in load");
                      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(); 
                       System.out.println(firstVisibleItem );
                       System.out.println(visibleItemCount );
                       System.out.println(totalItemCount);
                  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;
                   }
              }



      });

        doInBack dob=new doInBack();
            dob.execute();     
//        doback(webserv);
        Log.e("hi","doback called");



        }

我真的不明白为什么在调用AsyncTask时暂时添加项目。 帮帮我。谢谢你提前

1 个答案:

答案 0 :(得分:0)

首先让我知道,为什么要创建不同的ArrayList 来存储Object的数据。

 pic.clear();
 id.clear();
 name.clear();
 msg.clear();
 img.clear();
 profimg.clear();
 objid.clear();
 comment.clear();
 weburl.clear();
 adapter.clear();
 likes.clear();
 like_or_unlike.clear();

所以我的第一眼建议是创建一个包含所有值的单个对象,并将其放在 ArrayList<Object> 中。管理单个ArrayList比单个ArrayLists容易。表现也会提高。