android - custom filter in custom arrayadaper by icon -


i have custom 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();                        (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 = 0, l = filtredcontacts.size(); < 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 = 0, l = filtredcontacts.size(); < l; i++)                    {                           add(filtredcontacts.get(i));                     }                    notifydatasetchanged();            }      }      } 

but need filter items not name, icon. user chooses icon items should filtered , see list of items those, have icon on top. tried implement (pass int parameter) in performfiltering have charsequence parameter. , need implement feature not on typing value in edittext usual, icon int in activity , return list , see items filtered. sorry english.

i solved not using custom filter, implementing not elegant, workable solution. when return fragment int value adapter should filtered, this:

 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;                     (callitem call : adapter.getitems())                     {                         if (call.geticon() == r.drawable.incoming_call)                         {                                log.i("item 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;                     (callitem call : adapter.getitems())                     {                         if (call.geticon() == r.drawable.outgoing_call)                         {                                log.i("item 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);                  }              } } 

as mentioned, maybe not elegant, workable me. hope useful somebody.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -