1

I'm using CommonsGuy's drag n drop example and I am basically trying to integrate it with the Android notepad example.

Drag N Drop

Out of the 2 different drag n drop examples i've seen they have all used a static string array where as i'm getting a list from a database and using simple cursor adapter.

So my question is how to get the results from simple cursor adapter into a string array, but still have it return the row id when the list item is clicked so I can pass it to the new activity that edits the note.

Here is my code:

Cursor notesCursor = mDbHelper.fetchAllNotes();
    startManagingCursor(notesCursor);

    // Create an array to specify the fields we want to display in the list (only NAME)
    String[] from = new String[]{WeightsDatabase.KEY_NAME};

    // and an array of the fields we want to bind those fields to (in this case just text1)
    int[] to = new int[]{R.id.weightrows};

    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter notes = 
            new SimpleCursorAdapter(this, R.layout.weights_row, notesCursor, from, to);
    setListAdapter(notes);

And here is the code i'm trying to work that into.

public class TouchListViewDemo extends ListActivity {
private static String[] items={"lorem", "ipsum", "dolor", "sit", "amet",
                                                                "consectetuer", "adipiscing", "elit", "morbi", "vel",
                                                                "ligula", "vitae", "arcu", "aliquet", "mollis",
                                                                "etiam", "vel", "erat", "placerat", "ante",
                                                                "porttitor", "sodales", "pellentesque", "augue", "purus"};
private IconicAdapter adapter=null;
private ArrayList<String> array=new ArrayList<String>(Arrays.asList(items));

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    adapter=new IconicAdapter();
    setListAdapter(adapter);

    TouchListView tlv=(TouchListView)getListView();

    tlv.setDropListener(onDrop);
    tlv.setRemoveListener(onRemove);
}

private TouchListView.DropListener onDrop=new TouchListView.DropListener() {
    @Override
    public void drop(int from, int to) {
            String item=adapter.getItem(from);

            adapter.remove(item);
            adapter.insert(item, to);
    }
};

private TouchListView.RemoveListener onRemove=new TouchListView.RemoveListener() {
    @Override
    public void remove(int which) {
            adapter.remove(adapter.getItem(which));
    }
};

class IconicAdapter extends ArrayAdapter<String> {
    IconicAdapter() {
        super(TouchListViewDemo.this, R.layout.row2, array);
    }

    public View getView(int position, View convertView,
                                            ViewGroup parent) {
        View row=convertView;

        if (row==null) {                                                    
            LayoutInflater inflater=getLayoutInflater();

            row=inflater.inflate(R.layout.row2, parent, false);
        }

        TextView label=(TextView)row.findViewById(R.id.label);

        label.setText(array.get(position));

        return(row);
    }
}

}

I know i'm asking for a lot, but a point in the right direction would help quite a bit! Thanks

Cameron
  • 1,612
  • 2
  • 16
  • 24
  • Update: What I did so far was create a string array that contains the original names, cross reference that to the drag n drop array to find out which positions have changed, then update the rowId array with those changes. Ugly, but it works. Now i'm going to try and refine it to run more efficiently. – Cameron Feb 21 '11 at 00:09

1 Answers1

3

You cannot use a SimpleCursorAdapter with TouchListView, for the simple reason that a SimpleCursorAdapter cannot be modified. The Adapter has to change its data to reflect and drag and drop operations.

The simple but somewhat clunky solution would be to iterate over the Cursor and create an ArrayList of something out of that data, then use that ArrayList with an ArrayAdapter and TouchListView.

The slick but complex solution would be to create a decorating ListAdapter that sits between TouchListView and your SimpleCursorAdapter and knows about your drag and drop data changes and applies them on the fly. For example, if the user swaps positions 5 and 6, when TouchListView calls getView() to get position 5, the decorating adapter would know to get position 6's row from the SimpleCursorAdapter.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks! Just the man who I wanted to respond ;) Yeah I was afraid it wouldn't be too simple a solution. After looking into it, it seems like a custom adapter would be needed. Thanks for pointing me in the right direction. I think I have an idea on how to go about it now. I will update this thread if I get a working solution. – Cameron Feb 18 '11 at 23:42
  • @Cameron: "Thanks! Just the man who I wanted to respond ;)" -- well, you got lucky, in that I happened to look at this question. As the home page for `TouchListView` points out, support for it is on the `cw-android` Google Group -- if you ask questions there, I am far more likely to see them. – CommonsWare Feb 18 '11 at 23:49
  • @cameron did you manage to implement this? I'm designing my approach at the moment and am trying to understand the connections between the CursorAdapter -> ListAdapter -> TouchListView and how to implement an effective solution. – HGPB Jul 25 '12 at 12:11