I get an exception when dinamically add data to the viewPager
02-07 19:21:24.488 5577-5577/com.jamesb.encoderyapp E/AndroidRuntime: FATAL EXCEPTION: main Process: com.jamesb.encoderyapp, PID: 5577 java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 4, found: 8 Pager id: com.jamesb.encoderyapp:id/pager Pager class: class android.support.v4.view.ViewPager Problematic adapter: class com.jamesb.encoderyapp.adapters.ViewPagerAdapter at android.support.v4.view.ViewPager.populate(ViewPager.java:1000) at android.support.v4.view.ViewPager.populate(ViewPager.java:952) at android.support.v4.view.ViewPager$3.run(ViewPager.java:251) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767) at android.view.Choreographer.doCallbacks(Choreographer.java:580) at android.view.Choreographer.doFrame(Choreographer.java:549) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
I get data from API - 3 pictures,when this pictures swiped, I call getContent() method again and in this moment i get IllegalStateException .
try {
RestClient.getInstance(getActivity()).getContent(date, new Callback() {
@Override
public void onFailure(Request request, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Response response) throws IOException {
String json = response.body().string();
GsonBuilder gsonBuilder = new GsonBuilder();
mGson = gsonBuilder.create();
jsonParsed = mGson.fromJson(json, JsonParsed.class);
if (jsonParsed != null) mApodData.add(jsonParsed);
if (count < iteration) {
count++;
getContent();
} else {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
mAdapter = new ViewPagerAdapter(getActivity(), mApodData);
mViewPager.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
}
});
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
View Pager Adapter
public class ViewPagerAdapter extends PagerAdapter {
private Activity mActivity;
private ArrayList<JsonParsed> mApodData = new ArrayList<>();
private Utils mUtils;
public ViewPagerAdapter(Activity activity, ArrayList<JsonParsed> mApodData) {
this.mActivity = activity;
this.mApodData = mApodData;
mUtils = new Utils();
}
@Override
public int getCount() {
return mApodData.size();
}
@Override
public int getItemPosition(Object object) {
return POSITION_UNCHANGED;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public View instantiateItem(final ViewGroup container, final int position) {
LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View itemView = inflater.inflate(R.layout.item, container, false);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.mPic = (ImageView) itemView.findViewById(R.id.picture);
viewHolder.mPicTitle = (TextView) itemView.findViewById(R.id.picTitle);
viewHolder.mDescription = (TextView) itemView.findViewById(R.id.description);
itemView.setTag(viewHolder);
JsonParsed item = mApodData.get(position);
String descr = item.explanation;
String title = item.title;
String picUrl = item.hdurl;
viewHolder.mPicTitle.setText(title);
viewHolder.mDescription.setText(descr);
Picasso.with(mActivity)
.load(String.valueOf(picUrl))
.into(viewHolder.mPic);
viewHolder.mPic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mUtils.setImageAsWallpaper(mActivity, viewHolder.mPic);
}
});
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
static class ViewHolder {
ImageView mPic;
TextView mPicTitle;
TextView mDescription;
}
}
I've seen similar questions in google ,but I don't understand where I did not use notifyDataSetChanged()
and this throws exception.