Search code examples
androidlistviewandroid-viewholder

Android passing data on drag


What is the full-proof way of passing data with drag and drop?

In my situation I have two lists and i can drag element from one to another. So the list from which we drag already has data associated with it. From what I have searched the usual way is storing data by views setTag() and getTag() methods. This is also what I would do, but since both lists use view-holder pattern, the actual data would be stored in holder. And the holder would then be set as views tag. However I have also heard that you should never store data in views or in holder (im using view-holder pattern in list adapters).

So what would be the best way to pass data from one list to another?


Solution

  • Try to store data in root activity or fragment:

    public class MyActivity extends Activity
            {
                private ListView list1;
                private ListView list2;
                private int dragPosition;
    
    
        ...
            protected class myDragEventListener implements View.OnDragListener {
    
            // This is the method that the system calls when it dispatches a drag event to the
            // listener.
            public boolean onDrag(View v, DragEvent event) {
    
                // Defines a variable to store the action type for the incoming event
                final int action = event.getAction();
    
                // Handles each of the expected events
                switch(action) 
                {
    
                    case DragEvent.ACTION_DRAG_STARTED:
                        // save the position in one list
                        dragPosition = somePosition;
                            return true;
    
                    case DragEvent.ACTION_DROP:
    
                        //do what you want with other list
                        return true;
                }
            }
        }
    

    maybe, this will be helpful.