android – Drag and drop sorting of cursor adapter and list adapter-ThrowExceptions

Exception or error:

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.

How to solve:

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 –

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 –

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 –

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.

Leave a Reply

Your email address will not be published. Required fields are marked *