首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android简单的like和not like查询

Android简单的like和not like查询
EN

Stack Overflow用户
提问于 2012-07-03 02:58:55
回答 1查看 1.4K关注 0票数 1

我正在尝试将光标与托管查询一起使用,以筛选设备上的媒体内容

代码语言:javascript
复制
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子句使用传递给它的目录,以便过滤该文件夹和子文件夹中的音乐。

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

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

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-04 04:21:37

在回答我自己的问题时,下面是代码:

代码语言:javascript
复制
         //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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11299575

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档