目前我有一個ListView實現無盡的列表加載,因爲它會加載X數量的項目,然後當用戶滾動到底部,它執行一個AsyncTask調用來加載更多項目,然後將它們加載到列表中並進行更新。目前這一切都是有效的,但問題是爲了讓它工作,它必須非常低效地完成,直到列表變得越來越長,加載新項目需要更長的時間,因爲整個列表是「替換」每次,這顯然是不可取的。這裏是我目前從我的AsyncTask列表更新它得到一個成功的響應後:列表適配器不更新新項目
//this first line is what makes the loading so slow, but without it the list returns an empty set when calling notifyDataSetChanged() on the ListAdapter
TopPostFragment.postList = new ArrayList<Post>(TopPostFragment.postList);
TopPostFragment.postList.addAll(result); //result is the new items that were received from the AsyncTask
TopPostFragment.updateList();
顯然,這第一行是完全沒有必要的,但由於某些原因,如果我刪除列表中不正確更新該適配器將更新爲空列表。我updateList()在TopPostFragment方法如下:
public static void updateList()
{
if(listAdapter != null)
{
listAdapter.clear();
listAdapter.addAll(postList);
listAdapter.notifyDataSetChanged();
}
}
我有一種感覺,這個問題可能以某種方式來自我如何處理它在我的listAdapter幹,所以這裏是我的構造爲ListAdapter對我是如何處理它:
List<Post> postList = null;
public PostListAdapter(Context context, int layoutResourceId, List<Post> list, int whichList, int currentFeedID) {
super(context, layoutResourceId, list);
postList = list;
}
我已經從這裏的所有代碼中刪除了與該問題無關的代碼行。如果我將它複製到一個全新的列表中,它只是在更新列表的位置上做錯了什麼?
看到需求光標這兩個學家一個高效:https://gist.github.com/pskink/b21c932f405740011144和測試的ContentProvider https://gist.github.com/pskink/9ab862cdd0d6b0281ab4 – pskink