Bonjour, je cherche à implémenter une listview avec drag and drop. Je veux pouvoir ordonner les items de ma liste en les déplaçant simplement. J'ai trouvé tout un tas d'exemples sur internet, mais ils ne se basent pas sur la fonctionnalité de drag and drop d'android 3.0 et je ne veux pas utiliser de librairie. Je ne trouve aucune aide, voici le code:

MainActivity :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
public class MainActivity extends ListActivity {
private ListView lvSort = null;
private ArrayAdapter<String> sortAdapter = null;
private View layoutDropArea = null;
 
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 
    String[] sortCategories = {"time", "service", "equipment", "type", "alert", "system", "priority"};
    sortAdapter = new ArrayAdapter<String>(this, R.layout.list_item,R.id.textView1,
            sortCategories);
    setListAdapter(sortAdapter);
 
    lvSort = getListView();
    lvSort.setTextFilterEnabled(true);
 
    layoutDropArea = findViewById(R.id.dropTarget);
 
    setupDragDrop();
}
 
/**
 * Setup what to do when we drag list items
 */
public void setupDragDrop() {
    lvSort.setOnItemLongClickListener(new OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> arg0, View v,
                int position, long arg3) {
            TextView txtview = (TextView)v.findViewById(R.id.textView1);
            String value = txtview.toString();
            ClipData data = ClipData.newPlainText("procedure", value);
            v.startDrag(data, new DragShadowBuilder(v), null, 0);//!!
            return true;
        }
    });
    DragListener mDragListener = new DragListener();
    // mListView.setOnDragListener(mDragListener);
 
    layoutDropArea.setOnDragListener(mDragListener);
 
}
private boolean processDrop(DragEvent event, int newPosition) {
    ClipData data = event.getClipData();
    if (data != null) {
        if (data.getItemCount() > 0) {
            Item item = data.getItemAt(0);
            String value = item.toString();
            updateViewsAfterDropComplete(value, newPosition);
            return true;
        }
    }
    return false;
}
 
private void updateViewsAfterDropComplete(String listItem, int index) {
    Log.d("InsertItem", "Position: " + index);
    sortAdapter.insert(listItem, index);
    sortAdapter.notifyDataSetChanged();
}
 
private boolean processDragStarted(DragEvent event) {
    ClipDescription clipDesc = event.getClipDescription();
    if (clipDesc != null) {
        return clipDesc.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);
    }
    return false;
}
protected class DragListener implements OnDragListener {
 
    public boolean onDrag(View v, DragEvent event) {
        final int action = event.getAction();
        switch (action) {
        case DragEvent.ACTION_DRAG_ENTERED:
            // v.setBackgroundColor(Color.GRAY);
            return false;
 
        case DragEvent.ACTION_DRAG_EXITED:
            // v.setBackgroundColor(Color.TRANSPARENT);
            return true;
 
        case DragEvent.ACTION_DRAG_STARTED:
            return true;
 
        case DragEvent.ACTION_DRAG_LOCATION:
            // v.setVisibility(View.VISIBLE);
            //return false;
             return processDragStarted(event);
        case DragEvent.ACTION_DROP:
            v.setBackgroundColor(Color.TRANSPARENT);
            int newPosition = lvSort.pointToPosition(
                    (int) (event.getX()), (int) event.getY());
            Log.d("Position", Integer.toString(newPosition));
            if (newPosition != ListView.INVALID_POSITION)
                return processDrop(event, newPosition);
            else
                return false;
        default:
            return true;
 
        }
    }
 
}
}
activity_main.xml :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dropTarget"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
 android:layout_weight="1">
<ListView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@android:id/list" >
</ListView>
</LinearLayout>
list_item.xml :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
 
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />
 
</LinearLayout>
Normalement ce code devrait marcher, mais j'ai un problème. La liste s'affiche, mais quand je clique sur un item j'ai cette exception : java.lang.UnsupportedOperationException at com.example.dragdrop.MainActivity.updateViewsAfterDropComplete(MainActivity.java:77)
ça correspond à la ligne 58 dans le code du post.
Avez-vous une solution ?

Merci d'avance