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();}
这是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了,是空白
精慕HU
相关分类