0

我为我的自定义适配器使用过滤器。适配器类如下所示:

 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);,但没有帮助。什么是可能的解决方案?

4

1 回答 1

0

我的问题是适配器使用的 ArrayList 错误。修改后的解决方案,对我有用:

 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;
    FrequentlyUsedMethods faq;
    SparseArray<String> labels;
    Map<ContactItem, View> contactViewMap = ContactsViewMapSingletone.getInstance().getOrderViewMap();
    private Filter filter;
   public ContactAdaper(Context context,  int layoutResourceId,List<ContactItem> contacts, SparseArray<String> labels) {
       super(context, layoutResourceId, contacts);
       this.layoutResourceId = layoutResourceId;
       this.context = context;

       this.contacts = new ArrayList<ContactItem>();
       this.contacts.addAll(contacts);
       this.filtredContacts = new ArrayList<ContactItem>();
       this.filtredContacts.addAll(contacts);
       this.labels = labels;
       faq = new FrequentlyUsedMethods(context);

   }
   @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;
   }

   @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.contact_label=(TextView)row.findViewById(R.id.contact_label);
           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.getDays()==0)
           {
               this.remove(contact);
               faq.deleteContact(contact);
           }
           else 
           {
           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()));
                if (labels.get(position)!=null)
                {
                        holder.contact_label.setVisibility(View.VISIBLE);
                        holder.contact_label.setText(labels.get(position));
                }
                else
                        holder.contact_label.setVisibility(View.INVISIBLE);
           }
       }
       catch(Exception e)
       {
        e.printStackTrace();

       }
       contactViewMap.put(contact,row);
       return row;
   }
   public SparseArray<String> getSparseLabelsArray()
   {
       return this.labels;
   }
   public List<ContactItem> getItems() {
       return filtredContacts;
   }
   public Map<ContactItem, View> getViewContactBindItems() {
       return contactViewMap;
   }
   static class ContactHolder
   {
     TextView  contact_label;
    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)
                   {
                       results.values = contacts;
                       results.count = contacts.size();
                   }
                   else
                   {
                       final ArrayList<ContactItem> list =  new ArrayList<ContactItem>();
                       synchronized (this)
                       {
                           list.addAll(contacts);
                       }
                       filtredContacts.clear();
                       int count = list.size();
                       for (int i=0; i<count; i++)
                       {
                           final ContactItem pkmn = list.get(i);
                           if(pkmn.getName().toLowerCase(Locale.getDefault()).contains(constraint))
                           {
                               Log.i("found pref",pkmn.toString() );
                               filtredContacts.add(pkmn);
                           }
                       }

                       for(int i = 0, l = filtredContacts.size(); i < l; i++)
                       {
                           Log.i("filtered list",filtredContacts.get(i).toString());
                       }
                       results.values = filtredContacts;
                       results.count = filtredContacts.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));
                   }
                   notifyDataSetChanged();
           }

    }
}
于 2013-08-13T15:12:42.927 回答