在我的搜索视图中,当我使用退格键(删除)键删除查询文本时未返回任何结果请参阅下面的图片以便更好地理解
看到这里我搜索test1
这是我的适配器中的一个对象,我得到了结果..
好的,我在这里再次询问,但我没有这个名字的对象,所以没有得到任何结果,这很好
现在我在这里按了退格键(删除键),现在我的查询文本是test
,我的适配器中有一个带有此名称的对象但是没有得到任何回报
删除查询文本时调用我的onQueryTextChange
。怎么能解决这个问题?任何线索??
代码:
Search_View.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String Text) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if (newText.length() == 0) {
notesAdapter = new NotesAdapter(getActivity(), getData());
recyclerView.setAdapter(notesAdapter);
}
if ( notesAdapter != null) {
notesAdapter.getFilter().filter(newText);
}
return true;
}
});
my notesAdapter的过滤部分:
private static class UserFilter extends Filter {
private final NotesAdapter adapter;
private final List<Information> originalList;
private final List<Information> filteredList;
private UserFilter(NotesAdapter adapter, List<Information> originalList) {
super();
this.adapter = adapter;
this.originalList = new ArrayList<>(originalList);
this.filteredList = new ArrayList<>();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
filteredList.clear();
final FilterResults results = new FilterResults();
if (constraint == null || constraint.length() == 0) {
filteredList.addAll(originalList);
} else {
final String filterPattern = constraint.toString().toLowerCase().trim();
for (final Information user : originalList) {
if ( (user.title != null && user.title.toLowerCase().contains(filterPattern)) ||(user.address != null &&(user.address.toLowerCase().contains(filterPattern))) ||
(user.postType != null && (user.postType.toLowerCase().contains(filterPattern)) )|| (user.publisher != null&& (user.publisher.toLowerCase().contains(filterPattern)))
)
{
filteredList.add(user);
}
}
}
results.values = filteredList;
results.count = filteredList.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
adapter.filteredUserList.clear();
if ((ArrayList<Information>) results.values != null ) {
adapter.filteredUserList.addAll((ArrayList<Information>) results.values);
}
adapter.notifyDataSetChanged();
} }
答案 0 :(得分:0)
我在我的适配器中创建了一个方法来应用搜索功能。
这就是我所做的......
<强> CustomAdaperte 强>
//Original list to be used for adapter
private List<CustomerPaid> customerList;
//Same list used for search
private List<CustomerPaid> tempCustomerListForSearch;
public CustomAdapter(List<CustomerPaid> customerList) {
this.customerList = customerList;
tempCustomerListForSearch = new ArrayList<>();
tempCustomerListForSearch.addAll(customerList);
}
/**
* Filters the data stored in list
* @param textSearch
*/
public void filter(String textSearch){
textSearch = textSearch.toLowerCase(Locale.getDefault());
customerList.clear();
if (textSearch.length() == 0) {
customerList.addAll(tempCustomerListForSearch);
} else {
for (CustomerPaid customerPaid : tempCustomerListForSearch) {
if (customerPaid.getName().toLowerCase(Locale.getDefault()).contains(textSearch))
customerList.add(customerPaid);
}
}
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return customerList.size();
}
在您的活动中:
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
customAdapter.filter(newText);
return false;
}
});