我已经为 android 做了一个应用程序来列出名字等于例如 John 的联系人。但我需要查看与name1
OR的联系name2
,例如Peter OR John
。
我试过设置过滤器“ Peter OR John
”、“ Peter || John
”,但它不起作用。你能帮我吗?
you should be write your custom adapter and filter.
public class MyAdapter<T extends BaseEntity> extends ArrayAdapter<T> implements Filterable{
private MainClass.ObjectFilter filter;
private final List<T> objects;
public MyAdapter(List<T> objects){
enter code here
this.objects = objects;
getFilter();
}
@Override
public Filter getFilter() {
if (filter == null){
MainClass ofc = new MainClass(objects);
filter = ofc.getObjectFilter();
}
return filter;
}
enter code here
}
and custom filter
public class MainClass<T extends BaseEntity> extends BaseEntity {
private List<T> allObjectItemsArray; // all contact list
private List<T> filteredObjectItemsArray; // filtered contact list
public ObjectFilterMainClass(List<T> objects)
{
this.allObjectItemsArray = new ArrayList<T>();
this.allObjectItemsArray.addAll(objects);
this.filteredObjectItemsArray = new ArrayList<T>();
this.filteredObjectItemsArray.addAll(allObjectItemsArray);
}
public ObjectFilter getObjectFilter(){
return new ObjectFilter();
}
public class ObjectFilter extends Filter {
@Override
public FilterResults performFiltering(CharSequence constraint) {
// constraint search text --> Peter OR John
constraint = constraint.toString().toLowerCase();
FilterResults result = new FilterResults();
if (constraint != null && constraint.toString().length() > 0) {
ArrayList<T> filteredItems = new ArrayList<T>();
a: for (int i = 0; i < allObjectItemsArray.size(); i++) {
T m = allObjectItemsArray.get(i);
// cast m to your class
String fistnameAndLastName = ((your object class) m).getFirstName() + ((your object class) m).getLastName();
if (fistnameAndLastName.toLowerCase().contains(constraint)) {
filteredItems.add(m);
continue a;
}
}
result.count = filteredItems.size();
result.values = filteredItems;
} else {
synchronized (this) {
result.values = allObjectItemsArray;
result.count = allObjectItemsArray.size();
}
}
return result;
}
@Override
public void publishResults(CharSequence constraint, FilterResults results) {
filteredObjectItemsArray = (ArrayList<T>) results.values;
adapter.notifyDataSetChanged();
adapter.clear();
if (filteredObjectItemsArray != null) {
for (int i = 0; i < filteredObjectItemsArray.size(); i++)
adapter.add(filteredObjectItemsArray.get(i));
}
adapter.notifyDataSetInvalidated();
}
}
}
main point is get first and last name and check the constraint text.
after that if true add filteredItems list.
String fistnameAndLastName = ((your object class) m).getFirstName() + ((your object class) m).getLastName();
if (fistnameAndLastName.toLowerCase().contains(constraint))
{
filteredItems.add(m);
continue a;
and use :
EditText edt = (EditText) this.view.findViewById(R.id.edtTxtName);
edt.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
addFilterToList();
}
@Override
public void afterTextChanged(Editable s) {
}
});
public void addFilterToList() {
ListView listView = (ListView) this.view.findViewById(R.id.listView);
EditText edt = (EditText) this.view.findViewById(R.id.edtTxtName);
MyAdapter myA = (MyAdapter) //
(listView.getAdapter());
myA.getFilter().filter(edt.getText());
listView.setAdapter(myA);
}