如何使用EditText在Custom ArrayList中实现Filter,我正在使用ArrayList将JSON数据解析为ListView。
我想允许用户使用EditText中的Person Name或City过滤记录
public class Persons {
private String name;
private String city;
.......
// getters and setters
}
PersonsAdapter.java:
public class PersonsAdapter extends BaseAdapter implements Filterable {
ArrayList<Persons> arrayList;
ArrayList<Persons> filteredItems;
LayoutInflater inflater;
int Resource;
ViewHolder holder;
Context context;
private ItemFilter mFilter = new ItemFilter();
public PersonsAdapter(Context context, int resource, ArrayList<Persons> arrayList) {
this.context = context;
this.inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.Resource = resource;
this.arrayList = arrayList;
this.filteredItems = arrayList;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// convert view = design
View view = convertView;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(Resource, null);
holder.textName = (TextView) view.findViewById(R.id.textName);
holder.textCity = (TextView) view.findViewById(R.id.textCity);
view.setTag(holder);
}
else
{
holder = (ViewHolder) view.getTag();
}
holder.textName.setText(arrayList.get(position).getName());
holder.textCity.setText(arrayList.get(position).getCity());
return view;
}
static class ViewHolder {
TextView textName;
TextView textCity;
}
public Filter getFilter() {
return mFilter;
}
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
constraint = constraint.toString().toLowerCase();
FilterResults result = new FilterResults();
filteredItems = new ArrayList<Persons>();
if (constraint != null && constraint.toString().length() > 0) {
for (int i = 0, l = arrayList.size(); i < l; i++) {
Persons person = arrayList.get(i);
// here check with your property
if (person.getName().toLowerCase().contains(constraint))
filteredItems.add(person);
}
result.count = filteredItems.size();
result.values = filteredItems;
} else {
synchronized (this) {
result.values = arrayList;
result.count = arrayList.size();
}
}
return result;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredItems = (ArrayList<Persons>) results.values;
notifyDataSetChanged();
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return arrayList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return arrayList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}
答案 0 :(得分:1)
尝试使用以下代码来解决您的问题,并了解Filterable
接口
将现有代码中的以下行替换为不每次实例化
private ItemFilter mFilter = new ItemFilter();
要
private ItemFilter mFilter;
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<Persons> filterList = new ArrayList<Persons>();
for (int i = 0; i < filteredItems.size(); i++) {
Persons person = filteredItems.get(i);
String name = person.getName().toUpperCase();
String city = person.getCity().toUpperCase();
if (name.contains(constraint.toString().toUpperCase()) || city.contains(constraint.toString().toUpperCase())) {
filterList.add(person);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = filteredItems.size();
results.values = filteredItems;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
arrayList = (ArrayList<Persons>) results.values;
notifyDataSetChanged();
}
}
public Filter getFilter() {
if (itemFilter == null) {
itemFilter = new ItemFilter();
}
return itemFilter;
}
@Override
public long getItemId(int position) {
return arrayList.indexOf(getItem(position));
}