Drag and drop sorting of cursor adapter and list adapter

I'm very surprised there is such a small amount of info on drag and drop sorting with a cursor adapter and list adapter.

The closest post I have found on stackoverflow is this one:


But, it's not clear to me how to implement what CommonsWare suggests - clarification would be very helpful.

So far I am binding the cursor data to a list adapter and setting this as follows:

mMyCursorAdapter = new MyCursorAdapter(getActivity(), null);
getLoaderManager().initLoader(0, null, this);

The list is generated but I now wish to add drag and drop functionality to the list items.

I would like to know the best way to go about this from an architectural point of view and any pointers as to how to go about the development of the core functionality would also be useful.


This blog post by Jason McReynolds (including a sample project) helped me a whole lot.

It explains how to use Carl A. Bauer's Library Drag-Sort-ListView with a CursorAdapter and SqLite. It shows how to save the the ListView's newly ordered state in the database as well.

This can definitely be achieved and lucky for you most of the work has already been taken care of, but you will need to modify a class slightly to meet your specifications.

The default Android music app has all of the classes you'll need.

First, you'll need to grab their custom ListView that allows for dragging and dropping.

That can be found here - TouchInterceptor.java.

You'll also need to grab their custom Cursor that's used to actually move the items in your ListView. It's an inner class called NowPlayingCursor.

That can be found here - TrackBrowserActivity.java

NowPlayingCursor extends AbstractCursor and its used to return the queue. The method makeNowPlayingCursor() is specifcally where you'll write most of your own code. Instead of returning the queue, you'll need to return the items you interested in moving, whatever they may be.

In order to use the TouchInterceptor, you'll need to implement TouchInterceptor.DropListener.

   private TouchInterceptor.DropListener mDropListener =
        new TouchInterceptor.DropListener() {
        public void drop(int from, int to) {
               final NowPlayingCursor mNowPlayingCursor = (NowPlayingCursor) YOUR_CURSOR;
                mNowPlayingCursor.moveItem(from, to);
                // Call `notifyDataSetChanged` here.

You should also look their moveQueueItem method used to move an item from one index to another. This method is used in the NowPlayingCursor when onMove and moveItem are called.

That can be found here - MediaPlaybackService.java

So, there's some work to be done on your part, but this definitely possible.

Here is a library that hopefully will solve your problem, it enables drag and drop reordering of list items. Has an excellent demo that includes use cases for Fragments and Cursors


Key features:

  • Clean drag and drop (no visual glitches; I hope!).
  • Intuitive and smooth scrolling while dragging.
  • Support for heterogeneous item heights.
  • Public startDrag() and stopDrag() methods.
  • Public interface for customizing the floating View.

Need Your Help

Go-Scala-Go! What are the main differences?

c# programming-languages scala go

I just found this web page comparing some code written in Scala, C# and Go. I am astonished to see how close Scala and Go code looks like, much more than Scala code compared to C# code.

What is the correct way to join multiple path components into a single complete path in emacs lisp?

elisp filenames path-manipulation

Suppose I have variables dir and file containing strings representing a directory and a filename, respectively . What is the proper way in emacs lisp to join them into a full path to the file?