Drag N Drop utilizing simple cursor

岁酱吖の 提交于 2019-12-11 07:31:56

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/5047578/drag-n-drop-utilizing-simple-cursor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!