Search code examples
androidcursormedia-player

Android simple query with like and not like


I am trying to use a cursor with a managed query to filter media content on the device

String[] dirs = new String[] {"%"+ dir + "%"};

String[] musicdata = { BaseColumns._ID,
        MediaColumns.DATA,
        MediaColumns.DISPLAY_NAME,
        MediaColumns.SIZE };

musiccursor = getActivity().getContentResolver().query(
        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
        musicdata, 
        MediaColumns.DATA + " like ? ", 
        dirs, 
        MediaColumns.DATA + " asc");

The where clause of the query uses a directory that is passed to it in order to filter for music within that folder and sub folders.

What I would like to do is also include a "not like" within the same query.

The reason for doing this is that the user has the ability to exclude a folder from a list view, and that is stored in an array and written to a file in order to retain the selection. I would like the cursor query to take these exclusions into account whilst still being linked to the folder that they pass through to it.

Thanks in advance!


Solution

  • In answer to my own query, here is the code:

             //Build the where clause
             StringBuilder where = new StringBuilder();
    
             //first add in all values from the exclusion array
             for (int i = 0; i < excludedFolders.size(); i++) {
             where.append(MediaColumns.DATA + " not like ? ");
             where.append(" AND ");
             }
             //then add in the final like clause, e.g. the folder the user selected
             where.append(MediaColumns.DATA + " like ? ");
    
             //convert it to a string
             String selection = where.toString();
    
             System.out.println(selection);
             ////////////////////////////////////////////////////////////////////////////////////////////
    
             //Build the arguments.  the array is set to the size of the exlcusion list, plus 1 for the fodler selected
             String[] dirs = new String[excludedFolders.size()+1]; 
             System.out.println(excludedFolders.size()+1);
             //first add in a value for each excluded folder
             for (int i = 0; i < excludedFolders.size(); i++) {
                    dirs[i] = "%" + excludedFolders.get(i) + "%";
                    System.out.println(i + " " + dirs[i]);
                }
             //add the argument value for the like element of the query 
             dirs[excludedFolders.size()]="%"+ dir + "%";
             System.out.println("excludedFolders.size() " + dirs[excludedFolders.size()]);  
             //start building the cursor
             String[] musicdata = { BaseColumns._ID,
                    MediaColumns.DATA,
                    MediaColumns.DISPLAY_NAME,
                    MediaColumns.SIZE };
             //run the query             
             musiccursor = getActivity().getContentResolver().query(
                        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
                        musicdata, 
                        selection,
                        dirs, 
                        MediaColumns.DATA+ " asc");
             //done