我有一个列表视图,其中填充了arraylist。但是,无论何时我使用pull刷新,listview中的数据都会重复,即使我在填充之前清除了arraylist。这是我的arraylist
List<TimeItems> myList = new ArrayList<TimeItems>();
这就是我填充适配器的方式
for (int in = 0; in <= video.size() - 1; in ++) {
String a = vDancers.get(in);
String b = video.get(in);
String c = vName.get(in);
TimeLineItems item = new TimeLineItems(a,b,c);
myList.add(item);
}
Collections.sort(myList);
you = new MyVideoAdapter(getActivity(), myList,nLikes,nComments,nRepost);
//you.notifyDataSetChanged();
listView.setAdapter(you);
这就是我在pull-to-refresh
上清除myList的方法 listView.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
you = null;
nComments.clear();
nRepost.clear();
nRepost.clear();
myList.clear();
parseFetch();
listView.onRefreshComplete();
}
}, 5000);
}
});
你是我的BaseAdapter,parseFetch()是我加载网络数据的方法。如何在刷新之前完全清除myList,以便在listview中没有重复?谢谢你的帮助
答案 0 :(得分:2)
根据我看到的代码段,我留下了您使用ArrayAdapter
的印象。如果确实如此,那么为什么在初始化适配器myList
后操作you
?
您所能做的只是致电
you.clear()
// add your data with you.add(item)
答案 1 :(得分:1)
在列表适配器上调用notifyDataSetChanged()。
删除此行you = null;
myList.clear();
you.notifyDataSetChanged();
答案 2 :(得分:1)
我明白了。我还没有清除我用来填充myList的其他数组。在记录了各个阶段后,我意识到这些数组是重复的,所以我只需要清除它们。这是最后的工作。谢谢@ Lavkush2oct和@Vesko的帮助。
listView.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
you = null;
nComments.clear();
nRepost.clear();
nRepost.clear();
myList.clear();
vDancers.clear();
video.clear();
vName.clear();
you.notifyDataSetChanged();
parseFetch();
listView.onRefreshComplete();
}
}, 5000);
}
});