我想从Android语句中搜索带有空格的特定单词

时间:2012-11-02 09:04:41

标签: java android

我想通过触发api来搜索列表。

我目前正在使用indexof()进行搜索,但如果我在搜索后提供空格,则会搜索但是列表未正确更新。

例如:  列表1中的两个记录.Qtlist和Uses 2. android
 如果我搜索“和”,那么它必须只显示1.但它显示1.&两个。

我的功能

  private ArrayList<BusinessDetails> GetSearchAdapterData(String searchKeyword) {

            final ArrayList<BusinessDetails> listData = new ArrayList<BusinessDetails>();
            String searchWith = "";
            Log.i("BusinessArray  ", "size is "
                    + sitesList.getBusinessArray().size());

            list.clear();

            if (sitesList.getBusinessArray() != null
                    && sitesList.getBusinessArray().size() > 0) {

                for (int i = 0; i < sitesList.getBusinessArray().size(); i++) {
                    // String searchWith = "";
                    searchWith += sitesList.getBusinessArray().get(i).busName;
                    Log.i("businessName ", "Bus_Name is "
                            + sitesList.getBusinessArray().get(i).busName
                            + " startwith " + startsWith);

                    if (searchWith.toLowerCase().indexOf(searchKeyword.toLowerCase()) != -1) {
                        listData.add(sitesList.getBusinessArray().get(i));
                    }

                }
                Log.i("List data is inside loop call", "Listdata" + listData.size());
            } else {
                Log.i("sitesList.getBusinessArray() is zero", "size Zero");
            }

            return listData;
        }

我已完成的功能改为:

 private ArrayList<BusinessDetails> GetSearchAdapterData(String searchKeyword) {

            final ArrayList<BusinessDetails> listData = new ArrayList<BusinessDetails>();
            String searchWith = "";
            Log.i("BusinessArray  ", "size is "
                    + sitesList.getBusinessArray().size());


            if (sitesList.getBusinessArray() != null
                    && sitesList.getBusinessArray().size() > 0) {

                for (int i = 0; i < sitesList.getBusinessArray().size(); i++) {

                    searchWith += sitesList.getBusinessArray().get(i).busName;
                }

                for (int i = 0; i < sitesList.getBusinessArray().size(); i++) {
                    if (searchWith.matches(".*\\b" + searchKeyword + "\\b.*")) {
                        listData.add(sitesList.getBusinessArray().get(i));
                        Log.i("Match Word fromList", "" + listData.get(i).busName);
                    }
                }
                    Log.i("List data is inside loop call", "Listdata" + listData.size());
            } else {
                Log.i("sitesList.getBusinessArray() is zero", "size Zero");
            }

            return listData;
        }

任何帮助将不胜感激,所以请...帮助

2 个答案:

答案 0 :(得分:1)

如果我正确理解问题

sitesList.getBusinessArray().size() == 2  
sitesList.getBusinessArray().get(0).busName == "Qtlist and Uses"  
sitesList.getBusinessArray().get(1).busName == "android"

searchWith的第一个值将是“Qtlist and Uses”,searchWith的第二个值将是“Qtlist和Usesandroid”,因为您将busName追加到searchWith。因此,两者都匹配测试索引是正确的。

省略'+'并使用

是不够的
searchWith = sitesList.getBusinessArray().get(i).busName;

答案 1 :(得分:1)

您可以使用字符串匹配方法和reg ex来匹配整个单词。以下是您可以用于解决问题的小例子。

String[] ss={"Qtlist and Uses","android"};

        for(String s:ss){
            System.out.println("s is "+s+"  and it matches with "+s.matches(".*\\band\\b.*"));
        }

以下是该计划的输出:

s is Qtlist and Uses  and it matches with true
s is android  and it matches with false

此处\b已用于标记单词边界。希望它能解决你的问题。

编辑1:尝试替换以下代码:

if (searchWith.toLowerCase().indexOf(searchKeyword.toLowerCase()) != -1) {
                        listData.add(sitesList.getBusinessArray().get(i));
                    }

if (sitesList.getBusinessArray().get(i).busName.matches(".*\\band\\b.*")){
     listData.add(sitesList.getBusinessArray().get(i));
}

使用以下正则表达式可以匹配任何单词

".*\\banyword\\b.*"