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:
https://stackoverflow.com/a/5047618/317889
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);
setListAdapter(mMyCursorAdapter);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With