I am searching data that is coming from service through Edit Text but the searching is not done properly on every search its filter wrong data and some time list data is added more than one time in my layout can any one help me please.
Filter Class
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults(); // Holds the results of a filtering operation in values
ArrayList<CountriesModel> FilteredArrList = new ArrayList<CountriesModel>();
if (mOriginalValues == null) {
mOriginalValues = new ArrayList<CountriesModel>(ArrayListModel); // saves the original data in mOriginalValues
}
if (constraint == null || constraint.length() == 0) {
// set the Original result to return
results.count = mOriginalValues.size();
results.values = mOriginalValues;
} else {
constraint = constraint.toString().toLowerCase();
for (int i = 0; i < mOriginalValues.size(); i++) {
String data = mOriginalValues.get(i).getCountry_name();
if (data.toLowerCase().startsWith(constraint.toString())) {
FilteredArrList.add(new CountriesModel());
}
}
// set the Filtered result to return
results.count = FilteredArrList.size();
results.values = FilteredArrList;
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
ArrayListModel = (ArrayList<CountriesModel>) results.values;
notifyDataSetChanged();
}
}
}
Fragment:
EditTextSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.e( "Databe ", s.toString());
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// String txt= EditTextSearch.getText().toString();
Log.e( "Dataon ", s.toString());
adapter.getFilter().filter(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
/*String txt= EditTextSearch.getText().toString();
*/
Log.e( "Dataaf ", s.toString());
/* String text = EditTextSearch.getText().toString().toLowerCase(Locale.getDefault());
adapter.getFilter().filter(text);*/
}
});