我的getFilter
适配器类中有recyclerView
。如果搜索以“ A”开头的名称,则列表结果没有变化,它无法正常工作。假设我搜索以“ S”开头的名称,该名称将位于列表的顶部。我也尝试使用电话号码和密码进行相同的操作。它什么也没显示。这就是我正在尝试的:
EditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(adapter!=null){
adapter.getFilter().filter(s);
}
}
});
上面的代码我用来发送字符以获取过滤方法:
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String charString = constraint.toString().toLowerCase();
if(charString.isEmpty()){
mDataFiltered = mData;
}
else{
List<Guru> filteredList = new ArrayList<Guru>();
for(Guru row:mData){
if(row.getName().toLowerCase().contains(charString) ||
row.getMobile().contains(charString) || row.getOfficePincode().contains(charString)|| row.getHomePincode().contains(charString) ){
filteredList.add(row);
}
}
mDataFiltered = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = mDataFiltered;
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if(results.values!=null){
mDataFiltered = (List<Guru>) results.values;
notifyDataSetChanged();
}
}
};
这里我正在执行过滤器功能。如果您知道错误,请ping我。