harsh_v harsh_v -4 years ago 392
Java Question

java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged

I am building a simple gallery app.
The images are synced with the server.
Now i have a simple gridView displaying images and onClick the image opens inside a viewPager. Just like the android default gallery.

The problem is-

java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged!


I need a way to notify the adapter of the viewPager from background.
For loading data in the background i am using Koush/Ion


CLass ImageGrid (summary)


public class ImageGrid extends Fragment implements HttpConnectCallback, FutureCallback<JsonArray> {
...
..
.
gridView.setOnItemClickListener(CustomListener);
//onClick-> the following code executes

PagerFragment pagerFragment = new PagerFragment().newInstance(position);
...
..
.


When the PagerFragment is alive the data loading still continues in the background


Class PagerFragment (summary)


public class PagerFragment extends Fragment {
...
..
.
viewPager = (ViewPager) view.findViewById(R.id.splash_pager);

viewPager.setAdapter(new PagerAdapter);// a PagerAdapter


Now the source of images for adapters in both the classes is same.
i.e I have a
static ArrayList<String>
that contains the path to the images on disk.

Now if data in the list changes and i try to scroll viewPager following exception occurs

java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged!
at android.support.v4.view.ViewPager.populate(Unknown Source)
at android.support.v4.view.ViewPager.populate(Unknown Source)
at android.support.v4.view.ViewPager.smoothScrollTo(Unknown Source)
at android.support.v4.view.ViewPager.scrollToItem(Unknown Source)
at android.support.v4.view.ViewPager.setCurrentItemInternal(Unknown Source)
at android.support.v4.view.ViewPager.onTouchEvent(Unknown Source)
...
...
...

Answer Source

Using clone() method to copy the data from the ArrayList which i passed to the constructor of ViewPager's adapter finally solved the issue.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download