Drap Drop in Honeycomb

Hi let us know how Drag and drop has to be written in Honeycomb

Drag Listener:

lv.setOnItemLongClickListener(new OnItemLongClickListener() {

            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                final String title = (String) ((TextView) arg1).getText();

                // Set up clip data with the category||entry_id format.
                final String textData = String.format("%d||%d", mCatogery, arg2);
                ClipData data = ClipData.newPlainText(title, textData);
                arg1.startDrag(data, new MyDragBuilder(arg1), null, 0);

                return true;
            }
        });

Here when ever an item in ListView is Long Clicked we start the drag event /*arg1.startDrag(data, new MyDragShadowBuilder(arg1), null, 0);*/

MyDragBuilder.java :

private static class MyDragShadowBuilder extends View.DragShadowBuilder {
        //private static Drawable shadow;
        private View view;

        public MyDragShadowBuilder(View v) {
            super(v);
            view = v;
            //view.setBackgroundResource(android.R.color.background_light);

        }

        @Override
        public void onDrawShadow(Canvas canvas) {
            view.draw(canvas);
            //shadow.draw(canvas);
        }
    }

Did you saw ClipData Here is how the Data is Passed from one Fragment to Other.

DropEvent in other Fragment :

lv.setOnDragListener(new OnDragListener() {

            public boolean onDrag(View v, DragEvent event) {
                // TODO Auto-generated method stub
                if(event.getAction()==DragEvent.ACTION_DRAG_STARTED)
                    return processDragStared(event);
                else if(event.getAction() == DragEvent.ACTION_DROP)
                    return processDrop(event, lv);

                return false;
            }
        });

processDragStarted(event) :

protected boolean processDragStared(DragEvent event) {
        // TODO Auto-generated method stub
        ClipDescription clipDesc = event.getClipDescription();
        if (clipDesc != null) {
            return clipDesc.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);
        }
        return false;
    }

processDrop(event, View)

protected boolean processDrop(DragEvent event, ListView lv) {
        // TODO Auto-generated method stub

        ClipData data = event.getClipData();
        if(data!=null)
        {
            if(data.getItemCount()>0)
            {
                Item item = data.getItemAt(0);
                String textData = (String)item.getText();
                if(textData!=null)
                {
                    StringTokenizer tokenizer = new StringTokenizer(textData, "||");
                    if (tokenizer.countTokens() != 2) {
                        return false;
                    }
                    int category = -1;
                    int entryId = -1;
                    try {
                        category = Integer.parseInt(tokenizer.nextToken());
                        entryId = Integer.parseInt(tokenizer.nextToken());
                    } catch (NumberFormatException exception) {
                        return false;
                    }

                    titles.add(Category.getCategory(category).getCharecter(entryId).getName());
                    getTitles(lv);
                    return true;

                }
            }
        }

        return false;
    }

My Entire DropEvent Class
Fragment1.java :

package com.ni.sample.fragments;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;

import android.app.Fragment;
import android.content.ClipData;
import android.content.ClipDescription;
import android.content.ClipData.Item;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnDragListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Fragment1 extends Fragment{

    private ArrayList<String> titles = new ArrayList<String>();

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        titles.add("Akamaru");
        titles.add("Shikamaru");
        titles.add("Tobi");
        titles.add("Byakugan");
        Collections.sort(titles);
        //getTitles();
    }

    private void getTitles(ListView lv) {
        // TODO Auto-generated method stub
        Collections.sort(titles);
        lv.setAdapter(new ArrayAdapter<String>(getActivity(), R.layout.title_list_item,titles));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View view = inflater.inflate(R.layout.fragmenttwo, null);
        final ListView lv = (ListView)view.findViewById(R.id.listview);

        lv.setOnDragListener(new OnDragListener() {

            public boolean onDrag(View v, DragEvent event) {
                // TODO Auto-generated method stub
                if(event.getAction()==DragEvent.ACTION_DRAG_STARTED)
                    return processDragStared(event);
                else if(event.getAction() == DragEvent.ACTION_DROP)
                    return processDrop(event, lv);

                return false;
            }
        });

        getTitles(lv);

        return view;
    }

    protected boolean processDrop(DragEvent event, ListView lv) {
        // TODO Auto-generated method stub

        ClipData data = event.getClipData();
        if(data!=null)
        {
            if(data.getItemCount()>0)
            {
                Item item = data.getItemAt(0);
                String textData = (String)item.getText();
                if(textData!=null)
                {
                    StringTokenizer tokenizer = new StringTokenizer(textData, "||");
                    if (tokenizer.countTokens() != 2) {
                        return false;
                    }
                    int category = -1;
                    int entryId = -1;
                    try {
                        category = Integer.parseInt(tokenizer.nextToken());
                        entryId = Integer.parseInt(tokenizer.nextToken());
                    } catch (NumberFormatException exception) {
                        return false;
                    }

                    titles.add(Category.getCategory(category).getCharecter(entryId).getName());
                    getTitles(lv);
                    return true;

                }
            }
        }

        return false;
    }

    protected boolean processDragStared(DragEvent event) {
        // TODO Auto-generated method stub
        ClipDescription clipDesc = event.getClipDescription();
        if (clipDesc != null) {
            return clipDesc.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);
        }
        return false;
    }
}