在适配器中调用notifyDataSetChanged后,元素不会刷新

时间:2017-07-15 20:06:45

标签: java android android-recyclerview

我创建了一个适配器并传递了一个带有空列表的类:

@Provides
@Singleton
Posts providesItemsList() {
    Posts posts = new Posts();
    posts.items = new ArrayList<>();
    return posts;
}

在此之后,我做了一些网络工作并调用了适配器的方法:

public void setItems(Posts newPosts) {
    Log.i("mytag", "NewPosts items number in the setItems method: " + Integer.toString(newPosts.items.size()));
    posts.items.addAll(newPosts.items);
    Log.i("mytag", "Posts items number in the setItems method: " + Integer.toString(posts.items.size()));
    posts.profiles.addAll(newPosts.profiles);
    posts.groups.addAll(newPosts.groups);
    notifyDataSetChanged();
}

这就是getItemCount的样子:

@Override
public int getItemCount() {
    Log.i("mytag", "Posts items number in the getitemcount method: " + Integer.toString(posts.items.size()));
    return posts.items.size();
}

在日志中:

07-15 18:57:41.967 7481-7481/com.ovchinnikovm.android.vktop I/mytag: Posts items number in the getitemcount method: 0
07-15 18:57:41.967 7481-7481/com.ovchinnikovm.android.vktop I/mytag: Posts items number in the getitemcount method: 0
07-15 18:57:42.005 7481-7481/com.ovchinnikovm.android.vktop I/mytag: Posts items number in the getitemcount method: 0
07-15 18:57:42.005 7481-7481/com.ovchinnikovm.android.vktop I/mytag: Posts items number in the getitemcount method: 0
07-15 18:57:42.118 7481-7481/com.ovchinnikovm.android.vktop I/mytag: NewPosts items number in the setItems method: 20
07-15 18:57:42.118 7481-7481/com.ovchinnikovm.android.vktop I/mytag: Posts items number in the setItems method: 20

如何正确更新我的Recyclerview?

2 个答案:

答案 0 :(得分:-1)

尝试在clear()上调用List方法,然后致电addAll()

答案 1 :(得分:-1)

尝试重新初始化适配器。