I have getFilter
in my recyclerView
adapter class. It is not working properly if I search a name that starts with 'A' there is no change in the list results. Suppose I search a name that starts with 'S', the name will come to the top of the list. Also I try the same thing with phone number and pincode. It doesn't show anything. This is What I am trying:
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);
}
}
});
This above code I used to send the characters to get filter method:
@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();
}
}
};
Here I am doing the filter function. If you know the mistake just ping me.