我在我的应用程序中使用了 pulltorefresh 列表视图。当我调用 pulltoRefresh 时,我正在调用 AsyncTask。在调用它之前,我正在清除 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 时临时添加项目。帮助我。在此先感谢