我为我的自定义适配器使用过滤器。适配器类如下所示:
public class ContactAdaper extends ArrayAdapter<ContactItem> implements Filterable{
View view;
ContactHolder holder = null;
int color = 0;
Context context;
int layoutResourceId;
public static int rowHeight = 0;
private ViewSwitcher switcher;
List<ContactItem> contacts = null;
List<ContactItem> filtredContacts = null;
Map<ContactItem, View> contactViewMap = ContactsViewMapSingletone.getInstance().getOrderViewMap();
private Filter filter;
public ContactAdaper(Context context, int layoutResourceId,List<ContactItem> contacts) {
super(context, layoutResourceId, contacts);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.filtredContacts = contacts;
}
@Override
public ContactItem getItem(int arg0) {
return contacts.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public Filter getFilter()
{
if (filter == null)
filter = new ContactFilter();
return filter;
}
/** * метод для привязки элементов UI к данным конкретного контакта */
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
view = row;
if(row == null)
{
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.contact_item, parent, false);
holder = new ContactHolder();
holder.contactIcon = (ImageView)row.findViewById(R.id.contactIcon);
holder.contactName = (CustomTextView)row.findViewById(R.id.contactName);
holder.contactNumber = (CustomTextView)row.findViewById(R.id.contactNumber);
holder.contactDays = (CustomTextView)row.findViewById(R.id.contactDays);
row.setTag(holder);
}
else
{
holder = (ContactHolder)row.getTag();
}
ContactItem contact = filtredContacts.get(position);
try{
if (contact.getContactGroup()!=null)
holder.contactIcon.setBackgroundResource(contact.getContactGroup().getIcon());
else
holder.contactIcon.setBackgroundResource(contact.getIcon());
holder.contactName.setText(contact.getName());
holder.contactNumber.setText(contact.getNumber());
holder.contactDays.setText(Integer.toString(contact.getDays()));
}
catch(Exception e)
{
e.printStackTrace();
}
contactViewMap.put(contact,row);
return row;
}
static class ContactHolder
{
ImageView contactIcon;
CustomTextView contactName;
CustomTextView contactNumber;
CustomTextView contactDays;
}
@SuppressLint("DefaultLocale")
private class ContactFilter extends Filter
{
@Override
protected FilterResults performFiltering(CharSequence constraint)
{
FilterResults results = new FilterResults();
String prefix = constraint.toString().toLowerCase();
if (prefix == null || prefix.length() == 0)
{
ArrayList<ContactItem> list = new ArrayList<ContactItem>(filtredContacts);
results.values = list;
results.count = list.size();
}
else
{
final ArrayList<ContactItem> list = new ArrayList<ContactItem>();
synchronized (this)
{
list.addAll(filtredContacts);
}
int count = list.size();
final ArrayList<ContactItem> nlist = new ArrayList<ContactItem>(count);
for (int i=0; i<count; i++)
{
final ContactItem pkmn = list.get(i);
if(pkmn.getName().toLowerCase(Locale.getDefault()).contains(constraint))
nlist.add(pkmn);
}
results.values = nlist;
results.count = nlist.size();
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filtredContacts = (ArrayList<ContactItem>)results.values;
notifyDataSetChanged();
clear();
for(int i = 0, l = filtredContacts.size(); i < l; i++)
{
add(filtredContacts.get(i));
}
notifyDataSetInvalidated();
}
}
}
但是过滤不执行的问题。所以我的适配器设置正确,但没有任何过滤。我也添加了contancts_list.setTextFilterEnabled(true);
,但没有帮助。什么是可能的解决方案?