Android简单查询与喜欢和不喜欢

时间:2012-07-02 18:58:55

标签: android cursor media-player

我正在尝试使用带有托管查询的光标来过滤设备上的媒体内容

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

查询的where子句使用传递给它的目录,以便过滤该文件夹和子文件夹中的音乐。

我想要做的是在同一个查询中包含“不喜欢”。

这样做的原因是用户能够从列表视图中排除文件夹,并将其存储在数组中并写入文件以保留选择。我希望光标查询将这些排除项考虑在内,同时仍然链接到它们传递给它的文件夹。

提前致谢!

1 个答案:

答案 0 :(得分:1)

在回答我自己的查询时,这里是代码:

         //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
相关问题