title: Android Exception : ConcurentModificationException
body: <p>I am getting this exception inconsistently while listview loading on back press from details of the item, which we selected.</p>
  
  <p>when I am going on this listview page it never throws exception but When I will go into this listview item and delete/Update listview.</p>
  
  <p><strong>Here is my code</strong>.</p>
  
  <pre><code>if (result != null) {
      try {
          ListParser listParser = new ListParser(context);
          JSONObject jsonParseData = new JSONObject(result);
          JSONObject listJson = new JSONObject(jsonParseData.getString("GetAllResult"));
  
          if (listJson.getBoolean(getResources().getString(R.string.success))) {
  
              **listParser.getListData(listJson);** // parser class parses data into ARRAYLIST
  
              String[] ITEMS = new String[ListParser.listData.size()];
  
              for (int i = 0; i &lt; ITEMS.length; i++) {
                  ITEMS[i] = ListParser.listData.get(i).getName().toString();
                  mItemsID.add(ListParser.listData.get(i).getNameId().toString());
              }
  
              // Array to ArrayList
              mItems = new ArrayList&lt;String&gt;(Arrays.asList(ITEMS));
              mListSectionPos = new ArrayList&lt;Integer&gt;();
              mListItems = new ArrayList&lt;String&gt;();
  
              **synchronized (ListParser.listData) {
                  new Poplulate().execute(ListParser.listData);  
                  // parser class uses data from ARRAYLIST... I think exception is throwing from here.
              }**
  
          } else {
              CommonUtil.showValidationAlert(context, listJson.getString(getResources().getString(R.string.error)));
              Log.i(TAG, " " + listJson.getString(getResources().getString(R.string.error)));
          }
      } catch (JSONException e) {
          e.printStackTrace();
      }
  } else {
      ToastUtil.ToastShort(context, "Server Timed out");
  }
  </code></pre>
  
  <p><strong>Populate AsyncTask</strong> Code goes from here: </p>
  
  <pre><code>private class Poplulate extends AsyncTask&lt;ArrayList&lt;listModel&gt;, Void, Void&gt; {
  
      ...
  
      @Override
      protected void onPreExecute() {
          // show loading indicator
          showLoading(mListView, mLoadingView, mEmptyView);
          super.onPreExecute();
      }
  
      @Override
      protected Void doInBackground(ArrayList&lt;listModel&gt;... params) {
          mListItems.clear();
          mListSectionPos.clear();
          ArrayList&lt;listModel&gt; items = params[0];
          if (mItems.size() &gt; 0) {
  
              // NOT forget to sort array
              Collections.sort(ListParser.listData, new CustomComparator());
  
              String prev_section = "";
  
              synchronized (this) {
                  for (listModel current_item : items) {
                      String current_section = current_item.getName().substring(0, 1).toUpperCase(Locale.getDefault());
  
                      if (!prev_section.equals(current_section)) {
                          mListItems.add(current_section);
                          mListItems.add(current_item.getName());
                          // array list of section positions
                          mListSectionPos.add(mListItems.indexOf(current_section));
                          prev_section = current_section;
                      } else {
                          mListItems.add(current_item.getName());
                      }
                  }
              }
          }
          return null;
      }
  
      @Override
      protected void onPostExecute(Void result) {
          if (!isCancelled()) {
              if (mListItems.size() &lt;= 0) {
                  showEmptyText(mListView, mLoadingView, mEmptyView);
              } else {
                  setListAdaptor();
                  showContent(mListView, mLoadingView, mEmptyView);
              }
          }
          super.onPostExecute(result);
      }
  }
  </code></pre>
  
  <p><strong>Here is the stacktrace lines</strong></p>
  
  <pre><code>03-24 18:23:24.681: E/AndroidRuntime(6841): FATAL EXCEPTION: AsyncTask #1
  03-24 18:23:24.681: E/AndroidRuntime(6841): Process: com.smart.adlware, PID: 6841
  03-24 18:23:24.681: E/AndroidRuntime(6841): java.lang.RuntimeException: An error occured while executing doInBackground()
  03-24 18:23:24.681: E/AndroidRuntime(6841):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
  03-24 18:23:24.681: E/AndroidRuntime(6841):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
  03-24 18:23:24.681: E/AndroidRuntime(6841):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
  03-24 18:23:24.681: E/AndroidRuntime(6841):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
  03-24 18:23:24.681: E/AndroidRuntime(6841):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
  03-24 18:23:24.681: E/AndroidRuntime(6841):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
  03-24 18:23:24.681: E/AndroidRuntime(6841):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  03-24 18:23:24.681: E/AndroidRuntime(6841):     at java.lang.Thread.run(Thread.java:841)
  03-24 18:23:24.681: E/AndroidRuntime(6841): Caused by: java.util.ConcurrentModificationException
  03-24 18:23:24.681: E/AndroidRuntime(6841):     at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
  03-24 18:23:24.681: E/AndroidRuntime(6841):     at com.smart.adlware.CaregiverList$Populate.doInBackground(CaregiverList.java:324)
  03-24 18:23:24.681: E/AndroidRuntime(6841):     at com.smart.adlware.CaregiverList$Populate.doInBackground(CaregiverList.java:1)
  03-24 18:23:24.681: E/AndroidRuntime(6841):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
  03-24 18:23:24.681: E/AndroidRuntime(6841):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  03-24 18:23:24.681: E/AndroidRuntime(6841):     ... 4 more
  </code></pre>
  
  <p>So please assist me ASAP. I am stuck with this Exception.</p>