我正在尝试从播放列表中删除重复的歌曲。这是我用来从播放列表中删除歌曲的代码。
public static void removeFromPlaylist(final Context context, final long id,
final long playlistId) {
final Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId);
final ContentResolver resolver = context.getContentResolver();
int del = resolver.delete(uri, Playlists.Members.AUDIO_ID + " = ? ", new String[] {
Long.toString(id)
});
Log.e("DeleteFromPlaylist",""+del);
}
但问题在于它会删除所有歌曲。假设我在播放列表Playlist1中添加了3次'SongOne'。当我删除'SongOne'时应该只删除一次,但这里所有3'SongOne'都被删除了。我在日志中得到'3'。那么我应该对代码进行哪些修改,使ContentResolver.delete()
只删除一次?
谢谢!
答案 0 :(得分:1)
您可以使用以下代码,这不是您需要测试的完整代码,因为我在这个区域没有任何IDE。
final Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId);
final ContentResolver resolver = context.getContentResolver();
Cursor cursor = resolver.query(uri,null , Playlists.Members.AUDIO_ID + " = ? ", new String[] {
Long.toString(id)} , null , null );
List<Long> ids = new ArrayList<Long>();
if (cursor != null)
while ( cursor.moveToNext())
{
ids.add ( cursor.getLong(cursor.getColumnIndex("id"))); // you need get id of song, this might be id
}
for (int i = 1 ; i < ids.size() ; i++) // skip first index in list
resolver.delete(uri, "id" + " = ? ", new String[] {
Long.toString(ids.get(i)) });