Drag N Drop utilizing simple cursor

Posted by Cameron on Stack Overflow See other posts from Stack Overflow or by Cameron
Published on 2011-02-18T23:16:15Z Indexed on 2011/02/18 23:25 UTC
Read the original article Hit count: 232

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

© Stack Overflow or respective owner

Related posts about android

Related posts about drag-and-drop