Drag and drop sorting of cursor adapter and list adapter
Asked Answered
R

3

21

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://mcmap.net/q/661418/-drag-n-drop-utilizing-simple-cursor

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.

Removable answered 25/7, 2012 at 12:35 Comment(6)
There is no drag-and-drop support for ListView in Android. I had a library for this, which I discontinued after I could no longer reasonably maintain it, and it would not work with a CursorAdapter anyway. You can find other drag-and-drop ListView implementations on the Internet, though I doubt any of those will work with a CursorAdapter.Toxophilite
@Toxophilite Can you share the library so that we can use it in our projects? ThanksDateless
@Toxophilite Thanks for the reply. I haven't been very successful in my searching for the cursor drag/drop/sort but i have found a number of listview implementations. I would like to still implement what you suggested in the post above. Would you still consider it a good approach and if so would you be able to elaborate a little :) ?Removable
@Android_Crazy github.com/commonsguy/cwac-touchlistRemovable
"Would you still consider it a good approach and if so would you be able to elaborate a little :)" -- not really. I have never implemented this. What I wrote in that answer was a back-of-the-envelope solution (actually, two solutions IIRC). The decorator pattern is a standard SW design pattern. Perhaps you could consider updating your question with specific things that you do not understand, and perhaps somebody can answer those specific things.Toxophilite
@Toxophilite Thanks a lot for sharing...Dateless
I
3

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.

Intermixture answered 12/6, 2013 at 22:41 Comment(0)
A
1

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.

Aureus answered 4/10, 2012 at 10:41 Comment(0)
S
0

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

https://github.com/bauerca/drag-sort-listview

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.
Slope answered 22/11, 2012 at 6:2 Comment(1)
Looks promising I will try and have a look at this shortly. Thank you for the link.Removable

© 2022 - 2024 — McMap. All rights reserved.