Custom filtering in AutoCompleteTextView not working

后端 未结 1 1350
后悔当初
后悔当初 2021-01-25 04:07

I am extending the Array Adapter as it follows but I get still the old results can you please tell me what is the problem ?

public class Adaptor extends ArrayAda         


        
相关标签:
1条回答
  • 2021-01-25 04:29

    Your list contains items from "items" and you don't modify items in it, you have to remove positions from "items" and then call notifyDataSetChange, to restore lately all items you have to save previous items

    this is works fine:

    /**
     * Adapter wrapper to represent list of dialogs
     * @author Ryazantsev Dmitry
     * @email dilix90@gmail.com 2012
     */
    public class FriendsAdapter extends ArrayAdapter<User>
    {
        private final LayoutInflater inflater;
        private final ImageLoader il;
        private Context parentContext;
        private List<User> mData;
        private List<User> mOriginalData;
        public SimpleImageLoader sil;
    
        @Override
        public void add(User object)
        {
            if (mOriginalData != null)
                mOriginalData.add(object);
            else
                mData.add(object);
        }
    
        @Override
        public void remove(User object)
        {
            if (mOriginalData != null)
                mOriginalData.remove(object);
            else
                mData.remove(object);
        }
    
        @Override
        public int getCount()
        {
            return mData.size();
        }
    
        @Override
        public User getItem(int position)
        {
            return mData.get(position);
        }
    
        @Override
        public int getPosition(User item)
        {
            return mData.indexOf(item);
        }
    
        public FriendsAdapter(Context context, int textViewResourceId, List<User> objects)
        {
            super(context, textViewResourceId, objects);
            Log.v("refresh", context + " " + textViewResourceId + " " + objects);
            parentContext = context;
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            il = new ImageLoader(context);
            mData = objects;
            sil = new SimpleImageLoader(context, true, 64);
            sil.setOnUiThread(false);
        }
    
        /**
         * We have a custom view and need to organize it
         */
        @Override
        public View getView(final int position, View convertView, final ViewGroup parent)
        {
    
            ViewHolder holder;
            if (convertView == null)
            {
                convertView = inflater.inflate(R.layout.friends_list_row, null);
                holder = new ViewHolder();
                holder.name = (TextView) convertView.findViewById(R.id.friendName);
                holder.root = convertView.findViewById(R.id.root);
                holder.photo = (ImageView) convertView.findViewById(R.id.friendPhoto);
                holder.online = (ImageView) convertView.findViewById(R.id.online);
                convertView.setTag(holder);
            }
            else
                holder = (ViewHolder) convertView.getTag();
    
            User user = getItem(position);
            holder.online.setVisibility(user.isOnline() > 0 ? View.VISIBLE : View.GONE);
            if (user != null)
            {
                holder.name.setText(user.getFIO());
                holder.photo.setTag(user.getPhotoUrl());
                if (user.getPhotoBitmap() != null)
                    holder.photo.setImageBitmap(user.getPhotoBitmap());
                else
                {
                    holder.photo.setImageResource(R.drawable.contact_nophoto);
                    sil.displayImageUserAttach(holder.photo, user.getPhotoUrl(), user, null, false, null);
                }
            }
    
            // parent.setVisibility(position % 2 == 0?View.GONE:View.VISIBLE);
            return convertView;
        }
    
        private static class ViewHolder
        {
            public TextView name;
            public ImageView photo;
            public View root;
            public View online;
        }
    
        @Override
        public Filter getFilter()
        {
            return new Filter()
            {
                @SuppressWarnings("unchecked")
                @Override
                protected void publishResults(CharSequence constraint, FilterResults results)
                {
                    Log.v("filter", "filter finished");
                    mData = (List<User>) results.values;
                    if (results.count > 0)
                    {
                        notifyDataSetChanged();
                    }
                    else
                    {
                        notifyDataSetInvalidated();
                    }
                }
    
                @Override
                protected FilterResults performFiltering(CharSequence constraint)
                {
                    Log.v("filter", "filter perform");
                    if (mOriginalData == null)
                        mOriginalData = new ArrayList<User>(mData);
                    List<User> result;
                    FilterResults r = new FilterResults();
                    if (constraint == null || constraint.length() <= 0)
                        result = new ArrayList<User>(mOriginalData);
                    else
                    {
                        result = new ArrayList<User>();
                        for (int i = 0; i < mOriginalData.size(); i++)
                            if (constraint.length() > 0
                                    && mOriginalData.get(i).getFIO().toLowerCase()
                                            .contains(constraint.toString().toLowerCase()))
                                result.add(mOriginalData.get(i));
                    }
                    r.values = result;
                    r.count = result.size();
                    return r;
                }
            };
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题