如何检索给定ID的歌曲

时间:2016-12-25 12:26:45

标签: java android android-cursorloader

我尝试使用给定的歌曲ID从MediaStore.Audio.Media.EXTERNAL_CONTENT_URI检索歌曲:

@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {

    String selection = MediaStore.Audio.Media._ID + "=?";
    String[] ids = getDataFromDb();
    String[] test= {"151", "150"};
    return new CursorLoader(getActivity(), MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, selection, ids, null);
}

为了以防万一,我尝试了String[] ids = getDataFromDb();String[] test = {"151", "150"};

您可以在下方查看getDataFromDb()方法:

public String[] getDataFromDb(){
    SQLiteDatabase database = dbHelper.getReadableDatabase();
    String[] proj = {FavouritesContract.FavouritesEntry._ID, FavouritesContract.FavouritesEntry.SONG_ID};
    Cursor cursor = database.query(FavouritesContract.FavouritesEntry.TABLE_NAME, null, null, null, null, null, null);
    String[] ids = new String[cursor.getCount()];
    int i = 0;
    while(cursor.moveToNext()){
        String id = cursor.getString(cursor.getColumnIndex("song_id"));
        ids[i] = id;
        i++;
    }
    cursor.close();
    return ids;
}

String []填充id没有问题,如果只有一个id也不是问题,但是当CursorLoader开始检索多首歌曲(两个,三个或更多)时,我得到了错误:

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #4
              Process: comvoroninlevan.httpsgithub.simplymusic, PID: 24588
              java.lang.RuntimeException: An error occurred while executing doInBackground()
                  at android.os.AsyncTask$3.done(AsyncTask.java:309)
                  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                  at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                  at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                  at java.lang.Thread.run(Thread.java:818)
               Caused by: java.lang.IllegalArgumentException: Cannot bind argument at index 2 because the index is out of range.  The statement has 1 parameters.
                  at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:165)
                  at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
                  at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
                  at android.content.ContentResolver.query(ContentResolver.java:502)
                  at android.content.CursorLoader.loadInBackground(CursorLoader.java:64)
                  at android.content.CursorLoader.loadInBackground(CursorLoader.java:42)
                  at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:312)
                  at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
                  at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:57)
                  at android.os.AsyncTask$2.call(AsyncTask.java:295)
                  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                  at java.lang.Thread.run(Thread.java:818) 

会欣赏任何提示,链接或答案。 提前谢谢。

0 个答案:

没有答案