Search code examples
androiddrag-and-drop

How to send and receive multiple data and data-types through Clipdata in android drag and drop?


This code is for sending and receiving only one data through clipdata in drag and drop.

Send

ClipData.Item item = new ClipData.Item((CharSequence)number);

String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData("",mimeTypes, item); 
  View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(adImage);
                adImage.startDrag(dragData, shadowBuilder, adImage, 0);

Receive data

ClipData.Item item = event.getClipData().getItemAt(0);

Please tell me how to send multiple data through clipdata in drag and drop android.


Solution

  • Once again I got answer by myself. Do like this if you want to send multiple data with Clipdata.

    you need to send data in item if you want to send data through Clipdata.

    SEND DATA:

    String number="152468465";
    Int a=55;
    
    ClipData.Item item = new ClipData.Item((CharSequence)number);  
    // 1st item
    
    ClipData.Item item1 = new ClipData.Item((CharSequence)a);
    //2nd item and so on
    
    String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN}; 
    //Declare mime type according to your logic
    View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(adImage);
                adImage.startDrag(dragData, shadowBuilder, adImage, 1);
    // Here 1 is array index of Clipdata ;
    if you have one item then make it 0 and if you have more than one
    item then make it according to the array index .
    

    RECEIVE :

    ClipData.Item item = event.getClipData().getItemAt(0); //number
    ClipData.Item item1 = event.getClipData().getItemAt(1);  //a