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 に答える