我正在通过编辑文本搜索来自服务的数据,但搜索没有在每次搜索时正确完成,其过滤错误的数据和一些时间列表数据在我的布局中添加了多次,任何人都可以帮助我。
过滤类
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();
}
}
}
片段:
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);*/
}
});