カスタム アダプターにフィルターを使用します。アダプタ クラスは次のようになります。
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);
たが、役に立ちませんでした。可能な解決策は何ですか?