我遇到了一个我自己无法处理的问题,我已逐字检查了每一行代码,但它仍无效。
我有一个可拖动的listView,它代表一个播放列表(普通的MediaStore.Audio.Playlist)和drop listener,用于改变歌曲的顺序。
listView.setDropListener(new DragSortListView.DropListener() {
@Override
public void drop(int i, int i2) {
if(playlistId != -1) {
boolean moved = MediaStore.Audio.Playlists.Members.moveItem(getContentResolver(), playlistId, i, i2);
System.out.println("Item moved " + moved);
}
}
});
我还有contentObserver,它处理游标重新加载。
private class MyContentObserver extends ContentObserver {
MyContentObserver(Handler handler) {
super(handler);
}
@Override
public boolean deliverSelfNotifications() {
return true;
}
@Override
public void onChange(boolean selfChange, Uri uri) {
super.onChange(selfChange);
System.out.println("Updated");
}
}
当然手柄已注册
getContentResolver().registerContentObserver(Uri.parse("content://media"), true, contentObserver);
但这是一个问题 - moveItem总是返回true,但歌曲的顺序保持不变。在2.3.6上,moveItem完全不起作用,在4.4.2上 - 有时,在4.4.4的索尼平板电脑上 - 一切运行良好。我也尝试了相同结果的代码
public static final boolean moveItem(ContentResolver res,
long playlistId, int from, int to) {
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external",
playlistId)
.buildUpon()
.appendEncodedPath(String.valueOf(from))
.appendQueryParameter("move", "true")
.build();
ContentValues values = new ContentValues();
values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, to);
return res.update(uri, values, null, null) != 0;
}
答案 0 :(得分:0)
我已在我的播放列表详细信息上实施了拖放操作。大多数(如果不是全部来自这个论坛)我看起来比你的方法更复杂。 也许下面的代码片段对您有所帮助 在listview中的行上设置longclicklistener(播放列表中的轨道)
private final class MyItemLongClickListener implements OnItemLongClickListener {
public boolean onItemLongClick(AdapterView<?> arg0, View v,
int row, long rowid) {
if (android.os.Build.VERSION.SDK_INT > 10) {
Cursor crs = (Cursor) mAdapter.getItem(row);
int audio_id = crs.getInt(crs.getColumnIndex(MediaStore.Audio.Playlists.Members.AUDIO_ID));
int _id = crs.getInt(crs.getColumnIndex(MediaStore.Audio.Playlists.Members._ID));
int playorder_col = crs.getColumnIndexOrThrow(MediaStore.Audio.Playlists.Members.PLAY_ORDER);
String playorder = crs.getString(playorder_col);
String text = row + ":" + audio_id + ":" + lngplaylist_id + ":" +
playorder +
":" + _id;
ClipData.Item item = new ClipData.Item(text);
ClipData dragData = new ClipData(text, new String[]{ClipDescription.MIMETYPE_TEXT_PLAIN, ClipDescription.MIMETYPE_TEXT_PLAIN}, item);
dragData.addItem(new ClipData.Item(String.valueOf(row)));
mListView.setOnDragListener(new MyDragListener());
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(dragData, //data to be dragged
shadowBuilder, //drag shadow
v, //local data about the drag and drop operation
0 //no needed flags
);
// v.setVisibility(View.INVISIBLE);
return true;
}
return false;
}
class MyDragListener implements OnDragListener {
public int PositionOfDraggedItem = -1;
public int PositionOfItemDraggedOver = -1;
@Override
public boolean onDrag(View v, DragEvent event) {
int row = 0;
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
PositionOfDraggedItem = mListView.pointToPosition((int) (event.getX()), (int) event.getY());
return true;
case DragEvent.ACTION_DRAG_ENTERED:
row = mListView.getPositionForView(mListView);
return true;
case DragEvent.ACTION_DRAG_LOCATION:
case DragEvent.ACTION_DROP:
and so on
答案 1 :(得分:0)
names