【已解决】android recyclerview下拉刷新时为什么会清空上一次的列表然后再显示新的数据?而不是直接插入新数据?
Adapter:
List<Posts> mPosts;
Context mContext;
public MyRecycerAdapter(Context context, List<Posts> posts) {
mPosts = posts;
mContext = context;
}
public void addItem(int positionToAdd) {
mPosts.add(positionToAdd, new Posts());
notifyItemInserted(positionToAdd);
}
Volley Request:
StringRequest stringRequest = new StringRequest(Request.Method.GET, jsonUrl,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
mPostsJsonModel = new Gson().fromJson(response,
PostsJsonModel.class);
mPosts = mPostsJsonModel.getPosts();
}
Activity下拉刷新:
mMyRecycerAdapter.addItem(0);
为什么刷新时会清空第一页,然后显示第二页?而不是直接把新请求的数据插入第一页。。。
增加 mMyRecycerAdapter.notifyDataSetChanged();后刷新直接就崩溃了。
崩溃信息:
AndroidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException at com.**..Adapter.MyRecycerAdapter.onBindViewHolder(MyRecycerAdapter.java:61)
String imageUrl = mPosts.get(position).getAttachments().get(0).getImages().getThumbnail().getUrl();**是61行**
public void onBindViewHolder(final NormalViewHolder holder, int position) {
String titleItem = mPosts.get(position).getTitle();
holder.mTextView.setText(titleItem);
String imageUrl = mPosts.get(position).getAttachments().get(0).getImages().getThumbnail().getUrl();}
Posts Model:
public class Posts {
public List<Attachments> attachments;
public List<Attachments> getAttachments() {
return attachments;
}
public void setAttachments(List<Attachments> attachments) {
this.attachments = attachments;
}
}
Attachments Model:
public class Attachments {
private Images images;
public Images getImages() {
return images;
}
public void setImages(Images images) {
this.images = images;
}
}
Images Model:
public class Images {
private Thumbnail thumbnail;
public Thumbnail getThumbnail() {
return thumbnail;
}
public void setThumbnail(Thumbnail thumbnail) {
this.thumbnail = thumbnail;
}
}
Thumbnail Model:
public class Thumbnail {
private String url;
public void setUrl(String url) {
this.url = url;
}
public String getUrl() {
return url;
}
}
这是onCreate
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setHasFixedSize(true);
mMyRecycerAdapter = new MyRecycerAdapter(MainActivity.this,
mPosts);
mRecyclerView.setAdapter(mMyRecycerAdapter);
把setAdapter放到onCreate中然后就不显示recyclerview了,是空白
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为你每次onResponse都重新new了adapter并setAdapter,之前的数据当然没有了,只有最新的数据。