我正在尝试将光标与托管查询一起使用,以筛选设备上的媒体内容
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子句使用传递给它的目录,以便过滤该文件夹和子文件夹中的音乐。
我想要做的也是在同一查询中包含一个“不喜欢”。
这样做的原因是,用户能够从列表视图中排除文件夹,并且该文件夹存储在数组中并写入文件中,以便保留选择。我希望游标查询将这些排除项考虑在内,同时仍然链接到它们传递给它的文件夹。
提前感谢!
发布于 2012-07-04 04:21:37
在回答我自己的问题时,下面是代码:
//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");
//donehttps://stackoverflow.com/questions/11299575
复制相似问题