如何只有两个与Android Contentresolver匹配时删除一行

时间:2017-01-08 20:43:20

标签: java android android-contentprovider mediastore playlists

在我的音乐播放器中,播放列表可能包含两首相同的歌曲(用户将相同的歌曲添加两次)。 假设播放列表中的2次歌曲的id为7664。 从播放列表中删除此歌曲时,ContentResolver.delete(URI_OF_PLAYLIST, "audio_id=7664", null);会删除所有匹配项。 那我怎么只删除一个匹配?

提前致谢!

2 个答案:

答案 0 :(得分:0)

这就是我在我的应用新播放列表管理器中执行此操作的方法:

步骤1:创建一个带有音频ID的数组

第2步:在环绕此循环时,使用.lastindex()函数确定它是否存在于其他位置。 (否则该值应与位置相同)

步骤3:如果找到,请将音频Ids数组设置为null(您不想更改其大小)并从播放列表中删除

第4步:重新排序游戏订单

      public void dedupe_playlist(Context context, long playlist_id) {
    Cursor cursor = plist.getPlaylistTracks(context, playlist_id);
    ArrayList<String> audio_ids = new ArrayList<>();

    // build up the array with audio_id's
    int i = 0;
    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
        String audio_id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.AUDIO_ID));
        audio_ids.add(audio_id);

    }

    i = 0;
    int duplicates = i;
    boolean success = false;
    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
        String audio_id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.AUDIO_ID));
        duplicates = audio_ids.lastIndexOf(audio_id);
        if (duplicates > i) {
            success = true;
            cursor.moveToPosition(duplicates);
            int _id = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Playlists.Members._ID));
            removePlaylistTrack(context, playlist_id, _id);
            audio_ids.set(duplicates, null);
            cursor.moveToPosition(i - 1);        // move back to test again
        } else {
            i++;
        }
    }

    if (success) {
        renumber_playorder(context, playlist_id);
    }
    try {
        cursor.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}


     public void removePlaylistTrack(Context context, long selectedplaylist,
                                int _id) {

    ContentResolver resolver = context.getContentResolver();
    Uri newuri = MediaStore.Audio.Playlists.Members.getContentUri(
            "external", selectedplaylist);
    try {
        resolver.delete(newuri, MediaStore.Audio.Playlists.Members._ID + " = " + _id, null);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

答案 1 :(得分:0)

重新排序播放列表:

      public void renumber_playorder(Context context, long playlist_id) {
    Cursor cursor = getPlaylistTracks(context, playlist_id);
    Uri newuri = MediaStore.Audio.Playlists.Members.getContentUri(
            "external", playlist_id);
    ContentResolver resolver = context.getContentResolver();
    ContentValues values = new ContentValues();
    int i = 0;
    if (cursor != null && cursor.moveToFirst()) {
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            String _id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Playlists.Members._ID));
            String[] selection = {_id};
            values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, i);
            resolver.update(newuri, values, MediaStore.Audio.Playlists.Members._ID + " =? ", selection);
            i++;
        }
        cursor.close();
    }
}