4

I've been attempting to implement drag and drop features into my notes app using ItemTouchHelper however I am struggling to figure out how to get it to work with cursors. I am currently retrieving stored note data from the database using a cursor loader and a content provider, and the adapter receives it's data from the returned cursor as shown.

public void onBindViewHolder(RecyclerViewAdapter.MyViewHolder holder, int position) {
    mCursor.moveToPosition(position);
    String title = mCursor.getString(mCursor.getColumnIndex(NotesContract.COL_TITLE));
    String body = mCursor.getString(mCursor.getColumnIndex(NotesContract.COL_BODY));
    holder.titleText.setText(title);
    holder.bodyText.setText(body);
}

The problem I am having is that I don't know how I would keep track of the position of the moved items in the list when I drag and drop them. I can't change the order of the rows in the cursor. I have only seen examples where the data source for the adapter is a List. I have considered copying my cursor data into a list of Note objects, so that I could rearrange them to reflect the changes made by dragging and dropping, but this seems to defeat all you gain by using cursors and cursor loader. Also when cursor loader finishes loading it will return a new cursor with all the results in the original order again and populate the adapter accordingly, so any changes made to the order if my new List would be lost.

Does anyone know how to do this or am I going about this entirely the wrong way using cursors and a loader? Any help would be much appreciated. Thanks!

2 Answers2

2

I have done this by adding a column named sort_order in the table to keep track of the sort order. When you drag and drop you update the sort_order in the onMove method in ItemTouchHelper.SimpleCallback.

Magnus G
  • 81
  • 4
0

A good tutorial how to implement drag and drop and swipe to dismiss you can find here

What exactly do you need that cursor for? If you have any specific question just add a comment. I worked a lot with this features!

XxGoliathusxX
  • 816
  • 10
  • 29
  • Thanks for linking that tutorial, It looks helpful. I'm not sure if using Cursors is the best option or not. So I picked up Cursors when looking at a course from Udacity and they use a cursor loader to retrieve data from the database. After a bit of reading it seemed like a good way to do it as I believe Cursors don't load all the results in to memory, just point to them (please correct me if I'm wrong). Is there a better way to do this? In the above tutorial you posted a List is used as the data source for populating the adapter, is this a better way to go about it? –  Aug 19 '16 at 11:15
  • I need to ask, if a drag happens, how do switch the entries? Do you do it directly in the db via the pointer? – XxGoliathusxX Aug 19 '16 at 11:51
  • Yeah that's what I'm struggling with at the minute, I'm not sure how I would change the order of the entries seeing as you can't manipulate the cursor result once it's retrieved –  Aug 19 '16 at 14:34
  • its possible to do this in the db. but normally a db is not meant to save entries in a specific order. What you have to do is, select the entries you have from your db and put them in a list. After manipulating this list by the user you have to compare for every entry: Does it still exist? Did new ones got added by the user. Then you have to update the db. – XxGoliathusxX Aug 19 '16 at 14:44