我不知道发生了什么,但是当我在搜索栏中输入2个字的值时,我的searchView运行不正常。
例如:
我的列表视图显示如下
你救了我
高于一切
我并不感到羞耻
在搜索栏中,我想要搜索歌曲#34;你救了我"使用关键字"你"。结果很好,并显示完全匹配,但当我尝试输入"你保存"关键字结果不会出现在listview中的任何内容。当输入的值在2个单词之间有空格时,searchview似乎不起作用。
我该如何解决这个问题?
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
答案 0 :(得分:1)
试试这个
searchView.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
return true;
}
});
addTextChangeListener();
现在创建此方法
private void addTextChangeListener() {
searchView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence query, int start, int before, int count) {
query = query.toString().trim().toLowerCase();
final ArrayList<CityDataModel> filteredList = new ArrayList<>();
final CharSequence finalQuery = query;
new Thread(new Runnable() {
@Override
public void run() {
// Clear the filter list
filteredList.clear();
// If there is no search value, then add all original list items to filter list
if (TextUtils.isEmpty(finalQuery)) {
filteredList.addAll(cities);
} else {
// Iterate in the original List and add it to filter list...
for (CityDataModel item : cities) {
if (item.getCity_name().toLowerCase().contains(finalQuery.toString().toLowerCase())
) {
// Adding Matched items
filteredList.add(item);
}
}
}
// Set on UI Thread
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
// Notify the List that the DataSet has changed...
adapter = new SearchCityAdapter(SearchCityClass.this, filteredList);
recyclerSearchCity.setAdapter(adapter);
if (filteredList.size() == 0) {
tvNoCityFound.setVisibility(View.VISIBLE);
} else {
tvNoCityFound.setVisibility(View.GONE);
}
}
});
}
}).start();
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
答案 1 :(得分:0)
好的,伙计们。这篇帖子很旧了,但似乎没有人有兴趣回答我的问题,所以我决定回答我的旧问题,以防有人和我有同样的问题。
非常重要:使用 RecyclerView
而不是 ListView
以获得更好的性能。
创建 Recyclerview adapter
并将此类 Filterable
实现到您的适配器。
然后添加此代码。
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
final String text = charSequence.toString().toLowerCase();
List<SongsEntity> list;
if (text.isEmpty()) {
list = entitySongs; //"entitySongs" is your original arraylist
}else {
List<SongsEntity> songlist = new ArrayList<>();
for (SongsEntity song : entitySongs) {
if ((song.getTitle().toLowerCase().trim().contains(text)))
{
songlist.add(new SongsEntity(song.getId(), song.getTitle(), song.getArtist(),song.getLyrics()));
}
}
list = songlist;
}
FilterResults results = new FilterResults();
results.values = list;
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
songFiltered = (List<SongsEntity>) filterResults.values; // //"songFiltered" is your filtered arraylist
notifyDataSetChanged();
}
};
}
我不记得这个问题的确切原因,但我用上面的代码解决了这个问题。