Android MediaStore get cursor position by comparing with TITLE_KEY

谁都会走 提交于 2019-12-11 22:53:31

问题


I am using MediaStore's media content and have a SearchView to filter the content displayed in a listfragment.

I keep a reference to the cursor(mainCursor) when the query string is null (which means all rows) When user searches, i get new cursor based on the query

As per my understanding(I am very new to Cursor,DB & Contentprovider), the cursor will contain all the rows matching the query and the column to query is MediaStore.Audio.Media.TITLE, so i can see matching titles in list.

public void doQuery(String query) {
    // TODO Auto-generated method stub
    Log.e(TAG,"inside doQuery: "+query);
    String search = MediaStore.Audio.Media.TITLE + " LIKE '%" + query + "%'";
    Cursor cursorNew = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,columns, search , null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
    sendBroadcast(new Intent(MainActivity.SWAP_CURSOR));

}

Now when user presses list element, i getPosition() of the cursor and retrieve the TRACK_ID of selected item.

My question is: How to search the initial cursor, i.e., mainCursor with TRACK_ID got from cursorNew and to get the position of the matching TRACK_ID in mainCursor? I don't want to SELECT from the mainCursor, because i want to get the position of a specific TRACK from the un-filtered cursor

Thanks in advance


回答1:


If you are just interested in the position, why dont you put the audio_ids in an array

     ArrayList<String> audio_ids = new ArrayList<String>();

    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);
    }

and then use

        String audio_id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.AUDIO_ID));
        position = audio_ids.lastIndexOf(audio_id);

This example queries playlists. The above example would give you the last position if you had duplicates. (something you perhaps had not thought about?) I hope this gives you some ideas



来源:https://stackoverflow.com/questions/23930934/android-mediastore-get-cursor-position-by-comparing-with-title-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!