1

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)
...
...
...
harsh_v
  • 3,193
  • 3
  • 34
  • 53
  • I had the same error and found solution here [My solution](http://stackoverflow.com/a/32185084/1239911) – Amt87 Aug 25 '15 at 07:45

1 Answers1

1

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

harsh_v
  • 3,193
  • 3
  • 34
  • 53
  • That is a bad practice in my honest opinion. Instead, you should call `.notifyDataSetChanged()` after every change in the array. – Hindol Aug 06 '15 at 16:23
  • Well i have two fragments. First containing GridView to display image thumbnails. Now onClick loads another fragment with a ViewPager that displays images just like the phone's default album application .The difference is that the data is continually updated in the background and the methods that update the data are being called from the first fragment that i use to display thumbnails. Thus i don't have the access to adapter of ViewPager class from this class. – harsh_v Aug 06 '15 at 16:36
  • Well, you can keep it this way, OR, let the 2nd fragment load the data for itself. Just pass the `album id` or similar to the 2nd fragment from the first fragment. This approach is better because, if in future, you add a 3rd fragment and want to load the 2nd fragment from there, you don't need to duplicate the data loading code. – Hindol Aug 07 '15 at 07:31
  • I also wanted to do it the way you suggest but i cannot figure out how to notify the second fragment about the change in dataset. I tried to create an interface that shall notify about the dataset change to the adapter of any class which implements it but i didn't succeed so i am using this as workaround. I think i am missing something important. Can you tell me how the other applications handle the asynchronous changes to the database. Like Google photos. – harsh_v Aug 07 '15 at 08:57
  • I don't have experience in this exact scenario. I mostly use some kind of event bus (Otto, EventBus) to communicate. Never implemented it the native way, sorry. – Hindol Aug 07 '15 at 11:18
  • I'll have a look at event bus, that may be the solution. Thanks – harsh_v Aug 07 '15 at 12:27
  • There is also [DataSetObserver](http://developer.android.com/reference/android/database/DataSetObserver.html). – Hindol Aug 07 '15 at 14:42
  • helped me to figure out problem with Global data set for ViewPager Adapter. – ddnith Jun 09 '16 at 11:05