I have a list view showing some data. Now i want to incorporate the feature of drag and drop items in this listView. I mean i want to drag item from level 1 to level 3, and so on...
I got some examples of drag and drop listview, but all of them refer to an external library. I need to use only the official sdk and I know that drag and drop is available since android 3.0.
I tried to build it my self, but it is not working for me... most of the tutorial with this functionnality (drag and drop from android 3.0) are about drag and drop views and not items in a list.
Edit : I don't want library because since Honeycomb there is a feature on android to drag and drop views, and I want to use it for items. Level mean position in the list. Here is my code :
MainActivity.java :
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 :
<?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 :
<?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>
Normally I could drag the items in the list view and drop them at the wanted position, but I have a problem, when I do a long click on an item, I have this exception : java.lang.UnsupportedOperationException at com.example.dragdrop.MainActivity.updateViewsAfterDropComplete(MainActivity.java:77)
This line correspond to the folowing : "sortAdapter.insert(listItem, index);"
Please help with some clue.
Thanks & Regards