我的应用程序中有一个活动,它接受一个搜索词并返回包含该搜索词的文件名。我正在尝试修改此代码,以便它可以拆分搜索词并显示包含任何搜索词的文件。例如,如果搜索词是“big dog”,它将返回标题中包含“big”的文件以及标题中包含“dog”的文件。
代码的一部分是:
if (f.isDirectory()){
return true; // Don't discard any subdirectories
}
else {
String delimiter = " +"; /* delimiter */
searchname.searchname = searchname.searchname.toUpperCase();
//Split the search string
String [] tempname = searchname.searchname.split(delimiter);
//Array for the file names to be stored
boolean[] namestring = new boolean[tempname.length];
//Counter
int count;
count = 0;
for(int i=0; i<tempname.length; i++)
{
//While i is less than tempname size store filename to namestring array
namestring[i] = name.toUpperCase().contains(tempname[i]);
//Add one to count
count = +1;
//Once count = tempname length you can return all of the array values
if (count == tempname.length){
return namestring[i];
}
}
}
return false;
我的 java 非常基础,我可能遗漏了一些非常明显的东西。
非常感谢您的帮助
谢谢大家的快速回复。
我想我明白你们都在说什么。我修改了我的代码以显示:
public boolean accept(File dir, String name) {
File f = new File(dir, name); // Form the file path "dir/name".
String delimiter = " "; /* delimiter */
searchname.searchname = searchname.searchname.toUpperCase();
//Split the search string//
String [] tempname = searchname.searchname.split(delimiter);
//Array of Booleans
ArrayList<Boolean> matches = new ArrayList<Boolean>();
if (f.isDirectory()){
return true; // Don't discard any subdirectories
}
else {
for(int i=0; i<tempname.length; i++)
{
// Toast.makeText(getApplicationContext(), (tempname[i]), Toast.LENGTH_SHORT).show();
matches.add(name.toUpperCase().contains(tempname[i]));
}
}
return matches;
}});
但是在 Eclipse 中,它显示“返回匹配项;” 作为错误“类型不匹配:无法从 ArrayList 转换为布尔值”。如果我做出建议的更改,它会在“公共布尔接受(文件目录,字符串名称){”部分出现错误。有什么建议么?