【问题标题】:is there a difference between searching in txt files and searching in html file in java-android在 txt 文件中搜索和在 java-android 中的 html 文件中搜索有区别吗
【发布时间】:2016-01-19 17:32:00
【问题描述】:

[]这是对我的程序的简要说明:
1- 我做了一个药物名称列表视图(+8000 名称)
2-每个名称在资产文件夹中都有一个唯一的 html 文档
3- 我在列表视图中创建了一个搜索栏
[
]问题:我想改进搜索功能,以便它可以在每个 html 文件中进行搜索。
这是我的代码:

// Filter Class to search in titles and inside html files

public void filter(String charText) throws FileNotFoundException {

    charText = charText.toLowerCase(Locale.getDefault());
    druglist.clear();
    if (charText.length() == 0) {
        druglist.addAll(arraylist);

    } else {
        int i=-1;
        for (drugPopulation wp : arraylist) {
            i++;             
            if (wp.getitem().toLowerCase(Locale.getDefault()).contains(charText)||Searchfor(charText, arraylisthtml.get(i).getitem())) {
                druglist.add(wp);
            }
        }
    }
    notifyDataSetChanged();
}

*这是我的搜索功能:

private boolean Searchfor(String search, String s )  throws FileNotFoundException {
 String   path = "file:///android_asset/"+s;
    Boolean yes=false;

    final Scanner scanner = new Scanner(path);
    while (scanner.hasNextLine()) {
        final String lineFromFile = scanner.nextLine();
        if(lineFromFile.contains(search)) {
            // a match!
            yes=true;
            break;
        }
    }

    return yes;
}

[*]结果和问题:

1-当我运行应用程序并尝试使用此搜索实用程序时,它不会返回 true 结果(我认为 searchfor 函数总是返回 false ),在 html 文件中搜索和在 txt 文件中搜索之间是否有区别?是这些错误结果的原因吗?
2-搜索速度极慢,有办法改善吗?
提前致谢。

【问题讨论】:

    标签: java android html search android-5.0-lollipop


    【解决方案1】:

    在这种情况下,我可以看到您只检查文件是否包含某个单词 String search,因此您实际上不必逐行读取文件,您可以将其作为单个字符串读取,然后拨打.contains(search)就可以了,这应该会加快搜索速度

    还有:

    Searchfor(charText, arraylisthtml.get(i).getitem()) 
    

    您可以直接调用Searchfor(charText, wp.getitem()) 来代替Searchfor(charText, arraylisthtml.get(i).getitem()) 并摆脱i。

    但最重要的是,您可能应该“预处理”您的数据,我的意思是,如果您有办法对要搜索的文件进行分组和排序,请这样做,然后使用优化的算法,即字母顺序-二分查找。 如果您想提高搜索速度,您真的应该尝试这样做。祝你好运!希望我有帮助。

    【讨论】:

    • @Ravzan 非常感谢您的回答,我如何将 html 文件作为单个字符串读取,因为我认为它无法导入 java.io.IOException; java.nio.file.Files ;java.nio.file.Paths;
    猜你喜欢
    • 1970-01-01
    • 2017-08-15
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    • 2015-07-30
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多