-1

我想在通过触发 api 获得的列表中搜索。

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

示例:我在列表 1 中有两条记录。Qtlist 和使用 2.android
如果我搜索“和”,那么它必须只显示 1。但它显示 1。和 2。两者。

我的功能

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

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

4

2 回答 2

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 and Usesandroid”,因为您将 busName 附加到searchWith. 因此,两者都匹配 indexof 测试是正确的。

省略“+”并使用是否足够

searchWith = sitesList.getBusinessArray().get(i).busName;
于 2012-11-02T09:25:39.513 回答
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已经使用了mark这个词的边界。希望它能解决你的问题。

编辑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.*"
于 2012-11-02T10:03:13.547 回答