12

I am designing a drag and drop operation but I don't know how to access my data. Has anyone experience with Clip Data objects? Here is my code:

Starting the drag and drop:

ClipData dragData= ClipData.newPlainText("my", "test") );
                    v.startDrag(dragData, 
                            new MyDragShadowBuilder(v),
                              v, 0);

Listening on the events:

case DragEvent.ACTION_DROP:{
    if (event.getClipDescription().getLabel().equals("my"))
           Log.d("myLog","Data:"+event.getClipData()+" "+event.getClipData().getItemCount());
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Anthea
  • 3,741
  • 5
  • 40
  • 64

2 Answers2

26

not in every drag event can get the clip data, but some of them, such as ACTION_DROP type

enter image description here

    dropableCanvas.setOnDragListener(new OnDragListener() {
        @Override
        public boolean onDrag(View v, DragEvent event) {
            switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:
                return true;
            case DragEvent.ACTION_DROP:
                ClipData clipData = event.getClipData();
                //...
                return true;
            default:
                return false;
            }
        }

enter image description here

sam sha
  • 842
  • 9
  • 18
  • 1
    In case someone wonders which events contain what, you can find it in DragEvent documentation (see the first table): http://developer.android.com/reference/android/view/DragEvent.html – Pijusn Jul 23 '14 at 07:53
3

Before you start your drag set some clip data using the following code

ClipData.Item item = new ClipData.Item((CharSequence) v.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData(v.getTag().toString(), mimeTypes, item);

And then after you start dragging with v.startDrag(......); in the event DragEvent.ACTION_DROP you have to catch the clip data using the following code

String clipData = event.getClipDescription().getLabel().toString()

Once you have the clipData you can play around. This didn't return me null, check you at your end.

Sana
  • 9,895
  • 15
  • 59
  • 87