-2

我的应用程序中有一个活动,它接受一个搜索词并返回包含该搜索词的文件名。我正在尝试修改此代码,以便它可以拆分搜索词并显示包含任何搜索词的文件。例如,如果搜索词是“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 转换为布尔值”。如果我做出建议的更改,它会在“公共布尔接受(文件目录,字符串名称){”部分出现错误。有什么建议么?

4

4 回答 4

4

你错过的一件事是

count = +1;

在你的 for 循环中。它应该是

cout++;

或者

cout = count+1;
于 2013-08-23T01:49:30.607 回答
0

注释:

  1. count = +1 应该是 count +=1
  2. 如果要在for循环结束后返回,则不需要count,只需将return语句放在for循环之外
  3. 你想在你的评论中返回数组,但你只返回一个布尔值,而且总是最后一个
  4. 似乎您不需要返回数组,因为您只需要确定文件名是否包含任何术语,当文件名包含任何拆分数组时,您只需返回 true。
  5. 您的代码使用“+”作为分隔符,但您提到“big dog”作为分隔符为“”的示例
  6. 尝试使用此调试或日志记录进行故障排除,以分析哪个部分无法按预期工作
于 2013-08-23T03:48:05.580 回答
0

请注意,return namestring[i]它只返回一个值(如前所述,不会达到)。要返回您需要的所有数组值,将该方法声明为返回一个数组,然后只返回该数组,namestring

public boolean[] yourMethod()
{
     boolean[] namestring = ....
     //Your code
     return namestring;
}

我认为您应该使用 aList作为您的返回类型,在您的方法中实现一个Arraylist字符串(或布尔值,如果您愿意)。这样做的好处是您将有一个动态大小可用,即如果您的搜索词是"Big dog"并且您有 20 个文件并且其中 5 个匹配,那么您创建的数组只有 2 个元素,因此只会返回两个文件名。如果你把它作为一个列表来做,它会随着你添加更多而动态增长。

初始化:

List<String> matches = new ArrayList<String>();

然后,当您在循环中找到匹配项时(伪代码):

 //For each file name
     // For each search term
         // If name contains search term
              // Add to list
               matches.add(name)   // Actual code to add to list.

然后返回列表

return matches;
于 2013-08-23T02:29:38.747 回答
0

该函数应根据其定义返回布尔值: public boolean accept(File dir, String name) 但您返回 ArrayList。

这可能是你想要的:

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

    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();
            if(name.toUpperCase().contains(tempname[i])) {
                return true;
            }
        }               
    }

    return false;  
}
于 2013-08-25T09:47:21.680 回答