title: DynamicListView items disappear on drag & drop
body: <p>I followed this google tutorial and implemented a dynamic ListView for drag and drop
  <a href="https://www.youtube.com/watch?v=_BZIvjMgH-Q" rel="nofollow">https://www.youtube.com/watch?v=_BZIvjMgH-Q</a></p>
  
  <pre><code>listView = (DynamicListView) findViewById(R.id.listview);
  adapter = new StableArrayAdapter(this, R.layout.text_view, arraylist);
  listView.setAdapter(adapter);
  listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
  
  // added the items later and called notifyDataSetChanged();
      arraylist.add("item 1");
      arraylist.add("item 2");
      arraylist.add("item 3");
      arraylist.add("item 4");
  
  adapter.notifyDataSetChanged();
  </code></pre>
  
  <p>It shows up all the items in the listview but when I drag and drop the elements they disappear.</p>
  
  <p>If I add the elements before setting the adapter  <code>listView.setAdapter(adapter);</code> it works totally fine.</p>
  
  <p>Library Source code <a href="http://developer.android.com/shareables/devbytes/ListViewDraggingAnimation.zip" rel="nofollow">http://developer.android.com/shareables/devbytes/ListViewDraggingAnimation.zip</a></p>
  
  <p>How do i fix this?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>StableArrayAdapter.java</p>
  
  <pre><code>package com.example.android.navigationdrawerexample;
  
  import android.content.Context;
  import android.widget.ArrayAdapter;
  
  import java.util.HashMap;
  import java.util.List;
  
  public class StableArrayAdapter extends ArrayAdapter&lt;String&gt; {
  
      final int INVALID_ID = -1;
  
      int mRowLayout;
      List&lt;String[]&gt; mStrings;
      HashMap&lt;String, Integer&gt; mIdMap = new HashMap&lt;String, Integer&gt;();
  
      // constructor
      public StableArrayAdapter(Context context, int rowLayout, List&lt;String&gt; objects) {
          super(context, rowLayout, objects);
          for (int i = 0; i &lt; objects.size(); ++i) {
              mIdMap.put(objects.get(i), i);
          }
      }
  
      @Override
      public long getItemId(int position) {
          if (position &lt; 0 || position &gt;= mIdMap.size()) {
              return INVALID_ID;
          }
          String item = getItem(position);
          return mIdMap.get(item);
      }
  
      @Override
      public boolean hasStableIds() {
          return true;
      }
  }
  </code></pre>
  
  <p><strong>UPDATE 2</strong>
  I added the items to <em>objects</em> here as suggested and it works fine for now but isn't it the same as adding the items in MainActivity before setting the adapter?</p>
  
  <pre><code>public StableArrayAdapter(Context context, int rowLayout, List&lt;String&gt; objects) {
      super(context, rowLayout, objects);
  
      objects.add("item 1");
      objects.add("item 2");
      objects.add("item 3");
      objects.add("item 4");
      objects.add("item 5");
  
      for (int i = 0; i &lt; objects.size(); ++i) {
          mIdMap.put(objects.get(i), i);
      }
  }
  </code></pre>
  
  <p><strong>UPDATE 3</strong>
  The size of the objects is known only in the constructor
  and
  mIdMap.put("item 6", objects.size());    in the code has no effect.</p>
  
  <pre><code> // constructor
      public StableArrayAdapter(Context context, int rowLayout, List&lt;String&gt; objects) {
          super(context, rowLayout, objects);
  
          this.objects = objects;
  
          objects.add("item 1");
          objects.add("item 2");
          objects.add("item 3");
          objects.add("item 4");
          objects.add("item 5");
  
          for (int i = 0; i &lt; objects.size(); ++i) {
              mIdMap.put(objects.get(i), i);
          }
  
          mIdMap.put("item 6", objects.size());    // has no effect
  
      }
  
      public void addItem(String item) {
          int index = objects.size();
          mIdMap.put(item, index);
      }
  </code></pre>