Android ListView ArrayAdapter按两个名称筛选
我已经为android做了一个应用程序,列出了与名字相等的联系人,例如John。但是我需要查看与Android ListView ArrayAdapter按两个名称筛选,android,listview,filter,Android,Listview,Filter,我已经为android做了一个应用程序,列出了与名字相等的联系人,例如John。但是我需要查看与name1或name2的联系,例如Peter或John 我尝试过设置过滤器“Peter或John”、“Peter | | John”,但它不起作用。您能帮我吗?您应该编写自定义适配器和过滤器。 you should be write your custom adapter and filter. public class MyAdapter<T extends BaseE
name1
或name2
的联系,例如Peter或John
我尝试过设置过滤器“Peter或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);
}
公共类MyAdapter扩展ArrayAdapter实现可过滤{
私有MainClass.ObjectFilter过滤器;
私有最终列表对象;
公共MyAdapter(列出对象){
在这里输入代码
this.objects=对象;
getFilter();
}
@凌驾
公共过滤器getFilter(){
if(filter==null){
MainClass ofc=新的MainClass(对象);
filter=ofc.getObjectFilter();
}
回流过滤器;
}
在这里输入代码
}
和自定义过滤器
公共类MainClass扩展了BaseEntity{
私有列表allObjectItemsArray;//所有联系人列表
private List filteredObjectItemsArray;//筛选的联系人列表
公共对象筛选器包含类(列出对象)
{
this.allObjectItemsArray=新的ArrayList();
this.allObjectItemsArray.addAll(对象);
this.filteredObjectItemsArray=new ArrayList();
this.filteredObjectItemsArray.addAll(allObjectItemsArray);
}
public ObjectFilter getObjectFilter(){
返回新的ObjectFilter();
}
公共类ObjectFilter扩展了过滤器{
@凌驾
公共筛选器结果性能筛选(字符序列约束){
//约束搜索文本-->Peter或John
constraint=constraint.toString().toLowerCase();
FilterResults结果=新的FilterResults();
if(constraint!=null&&constraint.toString().length()>0){
ArrayList filteredItems=新的ArrayList();
a:for(int i=0;i
显示您的代码以帮助您。非常感谢,我会尝试一下