Android Drag and Drop getClipData returns always null
I am designing a drag and drop operation but I don't know how to access my data. Has anyone experience with Clip Da开发者_运维技巧ta 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());
not in every drag event can get the clip data, but some of them, such as ACTION_DROP type
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;
}
}
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.
精彩评论