0

我有自定义 ArrayAdapter:

     public class CallAdapter extends ArrayAdapter<CallItem> implements Filterable{
        View view;
        ContactHolder holder = null;
       int color = 0;
       Context context; 
       int layoutResourceId;    
       public static int rowHeight = 0;
        private ViewSwitcher switcher;
        List<CallItem> contacts = null;
        Map<ContactItem, View> contactViewMap = ContactsViewMapSingletone.getInstance().getOrderViewMap();
        private ArrayList<CallItem> filtredContacts;
        private Filter filter;
       public CallAdapter(Context context,  int layoutResourceId,List<CallItem> contacts) {
           super(context, layoutResourceId, contacts);
           this.layoutResourceId = layoutResourceId;
           this.context = context;
           this.contacts = contacts;
           this.filtredContacts = new ArrayList<CallItem>();
           this.filtredContacts.addAll(contacts);
       }
       @Override
       public CallItem getItem(int arg0) {
           return contacts.get(arg0);
       }

       @Override
       public long getItemId(int arg0) {
           return arg0;
       }
       /**  *   метод для привязки элементов 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.call_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();
           }

           CallItem contact = contacts.get(position);
           try
           {

                holder.contactIcon.setBackgroundResource(contact.getIcon());
                holder.contactName.setText(contact.getName());
                holder.contactNumber.setText(contact.getDays());
           }
           catch(Exception e)
           {
                    e.printStackTrace();
           }


           return row;
       }
       @Override
       public Filter getFilter()
       {
           if (filter == null)
               filter = new CallFilter();

           return filter;
       }
       static class ContactHolder
       {
        ImageView contactIcon;
        CustomTextView contactName;
        CustomTextView contactNumber;
        CustomTextView contactDays;
       }
  @SuppressLint("DefaultLocale")
    private class CallFilter extends Filter
       {
               @Override
               protected FilterResults performFiltering(CharSequence constraint)
               {   

                   Log.i(" filtered value", constraint.toString());
                   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<CallItem> list =  new ArrayList<CallItem>();
                       synchronized (this)
                       {
                           list.addAll(contacts);
                       }
                       filtredContacts.clear();
                       int count = list.size();
                       for (int i=0; i<count; i++)
                       {
                           final CallItem pkmn = list.get(i);
                           if(pkmn.getName().toLowerCase(Locale.getDefault()).contains(constraint))
                           {
                               Log.i("found pref",pkmn.toString() );
                               filtredContacts.add(pkmn);
                           }
                       }
                       Log.i("prefix",prefix);
                       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<CallItem>)results.values;
                   notifyDataSetChanged();
                   clear();
                   for(int i = 0, l = filtredContacts.size(); i < l; i++)
                   {  

                       add(filtredContacts.get(i));

                   }
                   notifyDataSetChanged();
           }

    }

    }

但我需要过滤项目不是按名称,而是按图标。所以用户选择应该过滤哪些项目的图标,并查看项目列表,这些项目在顶部有这个图标。我试图在中实现这个(将 int 作为参数传递),performFiltering但它必须获取CharSequence参数。而且我需要实现此功能,而不是像往常一样在 EditText 中键入值,而是在另一个 Activity 中获取 icon int 并返回我的列表并查看过滤的项目。对不起我的英语不好。

4

1 回答 1

0

我不是通过使用自定义过滤器来解决它,而是实现可能不是那么优雅但可行的解决方案。当我返回到我的 Fragment 时,应该过滤适配器的 int 值,我这样做:

 public void onActivityResult(int requestCode, int resultCode, Intent data) 
         {
               if (requestCode==1)
             { 
                 if(resultCode == 0)
                 {
                     adapter.clear();
                     ArrayList<CallItem> filtered = new ArrayList<CallItem>();
                    int a=0;
                    for (CallItem call : adapter.getItems())
                    {
                        if (call.getIcon() == R.drawable.incoming_call)
                        {   
                            Log.i("item to filter", call.toString());
                            adapter.insert(call, a);
                            a++;
                        }
                        else 
                        {
                            filtered.add(call);
                        }
                    }
                    adapter.addAll(filtered);
                    adapter.notifyDataSetChanged();

                 }
                 else if (resultCode==1)
                 {
                     adapter.clear();
                     ArrayList<CallItem> filtered = new ArrayList<CallItem>();
                    int a=0;
                    for (CallItem call : adapter.getItems())
                    {
                        if (call.getIcon() == R.drawable.outgoing_call)
                        {   
                            Log.i("item to filter", call.toString());
                            adapter.insert(call, a);
                            a++;
                        }
                        else 
                        {
                            filtered.add(call);
                        }
                    }
                    adapter.addAll(filtered);
                    adapter.notifyDataSetChanged();
                 }
                 else if(resultCode==2)
                 {
                     adapter = new CallAdapter(getActivity(),
                               R.layout.call_item, faq.getDisplayCalls(getActivity()));
                    contancts_list.setAdapter(adapter);
                 }
             }
}

正如我所提到的,也许它不是那么优雅,但对我来说是可行的。希望它对某人有用。

于 2013-08-16T09:54:09.877 回答