我想在通过触发 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;
}
任何帮助将不胜感激,请...帮助