title: ListFragment IllegalStateException: "The content of the adapter has changed but ListView did not receive the notification"
body: <p>I read some answers to this, but I don't see how I could apply the solution to my problem.</p>
  
  <p>To illustrate when the problem happens, I have a ViewPager with 3 tabs each corresponding to a fragment. The first fragment has a ListView. When I very quickly swipe between the two other tabs, my application crashes and I get this error. I understand that my Fragment continues to be reloaded and where it is happening. I'm not sure how to fix it though.</p>
  
  <p>The code in question:</p>
  
  <pre><code>@Override
  public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
  
      feed = getListView();
  
      feedItems = new ArrayList&lt;FeedItem&gt;();
      listAdapter = new FeedListAdapter(getActivity(), feedItems);
      feed.setAdapter(listAdapter);
  
      new Thread(new Runnable() {
          public void run() {
              for (int i = 0; i &lt; 9; i++) {
                  FeedItem item = new FeedItem("Status " + i);
  
                  feedItems.add(item);
             }
  
             getActivity().runOnUiThread(new Runnable() {
                 public void run() {
                     listAdapter.notifyDataSetChanged();
                 }
             }
          }
      }).start();
  }
  </code></pre>
  
  <p>The code above is pretty textbook in regards to how Stack Overflow will say to split things between different threads (process on worker thread, then notify about or do UI changes on UI thread). </p>
  
  <p>The culprit here is <code>feedItems.add(item);</code>.</p>
  
  <p>I've read about adding the items to the ArrayList in the UI thread, but if I'm adding them one at a time, it feels as though I'd be bouncing back and forth between the background thread and UI thread for each iteration of the loop. A quick solution would be to simply have two ArrayLists, one I use in the for loop and one that is ultimately used by the adapter when I basically copy one of the ArrayLists to the other and then <code>listAdapter.notifyDataSetChanged();</code></p>
  
  <p>I can't say I'm particularly fond of the above solution. It seems a tad tacky. At the core, this feels like a multithreading issue and I don't want to back up the UI thread at all by copying an ArrayList into another one before notifying the adapter of the change. Personally, I think I need to have some synchronization somewhere, but I'm new to this and not sure how I'd get that set up. </p>
  
  <p>An answer would really clear a lot up and also be very beneficial to many others that run into this, I think.</p>