title: Drag and drop ListView without library
body: <p>I have a list view showing some data. Now i want to incorporate the feature of drag and drop items in this listView. I mean i want to drag item from level 1 to level 3, and so on...</p>
  
  <p>I got some examples of drag and drop listview, but all of them refer to an external library. I need to use only the official sdk and I know that drag and drop is available since android 3.0. </p>
  
  <p>I tried to build it my self, but it is not working for me... most of the tutorial with this functionnality (drag and drop from android 3.0) are about drag and drop views and not items in a list.</p>
  
  <p><strong>Edit :</strong> I don't want library because since Honeycomb there is a feature on android to drag and drop views, and I want to use it for items. Level mean position in the list.
  Here is my code :</p>
  
  <p>MainActivity.java :</p>
  
  <pre><code>public class MainActivity extends ListActivity {
  private ListView lvSort = null;
  private ArrayAdapter&lt;String&gt; sortAdapter = null;
  private View layoutDropArea = null;
  
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      String[] sortCategories = {"time", "service", "equipment", "type", "alert", "system", "priority"};
      sortAdapter = new ArrayAdapter&lt;String&gt;(this, R.layout.list_item,R.id.textView1,
              sortCategories);
      setListAdapter(sortAdapter);
  
      lvSort = getListView();
      lvSort.setTextFilterEnabled(true);
  
      layoutDropArea = findViewById(R.id.dropTarget);
  
      setupDragDrop();
  }
  
  /**
   * Setup what to do when we drag list items
   */
  public void setupDragDrop() {
      lvSort.setOnItemLongClickListener(new OnItemLongClickListener() {
          public boolean onItemLongClick(AdapterView&lt;?&gt; arg0, View v,
                  int position, long arg3) {
              TextView txtview = (TextView)v.findViewById(R.id.textView1);
              String value = txtview.toString();
              ClipData data = ClipData.newPlainText("procedure", value);
              v.startDrag(data, new DragShadowBuilder(v), null, 0);//!!
              return true;
          }
      });
      DragListener mDragListener = new DragListener();
      // mListView.setOnDragListener(mDragListener);
  
      layoutDropArea.setOnDragListener(mDragListener);
  
  }
  private boolean processDrop(DragEvent event, int newPosition) {
      ClipData data = event.getClipData();
      if (data != null) {
          if (data.getItemCount() &gt; 0) {
              Item item = data.getItemAt(0);
              String value = item.toString();
              updateViewsAfterDropComplete(value, newPosition);
              return true;
          }
      }
      return false;
  }
  
  private void updateViewsAfterDropComplete(String listItem, int index) {
      Log.d("InsertItem", "Position: " + index);
      sortAdapter.insert(listItem, index);
      sortAdapter.notifyDataSetChanged();
  }
  
  private boolean processDragStarted(DragEvent event) {
      ClipDescription clipDesc = event.getClipDescription();
      if (clipDesc != null) {
          return clipDesc.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);
      }
      return false;
  }
  protected class DragListener implements OnDragListener {
  
      public boolean onDrag(View v, DragEvent event) {
          final int action = event.getAction();
          switch (action) {
          case DragEvent.ACTION_DRAG_ENTERED:
              // v.setBackgroundColor(Color.GRAY);
              return false;
  
          case DragEvent.ACTION_DRAG_EXITED:
              // v.setBackgroundColor(Color.TRANSPARENT);
              return true;
  
          case DragEvent.ACTION_DRAG_STARTED:
              return true;
  
          case DragEvent.ACTION_DRAG_LOCATION:
              // v.setVisibility(View.VISIBLE);
              //return false;
               return processDragStarted(event);
          case DragEvent.ACTION_DROP:
              v.setBackgroundColor(Color.TRANSPARENT);
              int newPosition = lvSort.pointToPosition(
                      (int) (event.getX()), (int) event.getY());
              Log.d("Position", Integer.toString(newPosition));
              if (newPosition != ListView.INVALID_POSITION)
                  return processDrop(event, newPosition);
              else
                  return false;
          default:
              return true;
  
          }
      }
  
  }
  }
  </code></pre>
  
  <p>activity_main.xml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/dropTarget"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
   android:layout_weight="1"&gt;
  &lt;ListView
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:id="@android:id/list" &gt;
  &lt;/ListView&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>list_item.xml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" &gt;
  
  &lt;TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="TextView" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Normally I could  drag the items in the list view and drop them at the wanted position, but I have a problem, when I do a long click on an item, I have this exception : java.lang.UnsupportedOperationException at com.example.dragdrop.MainActivity.updateViewsAfterDropComplete(MainActivity.java:77)</p>
  
  <p>This line correspond to the folowing : "sortAdapter.insert(listItem, index);"</p>
  
  <p>Please help with some clue.</p>
  
  <p>Thanks &amp; Regards</p>