AutoCompleteTextView中的自定义筛选无法正常工作

时间:2012-07-20 17:54:51

标签: android

我正在扩展阵列适配器,但是我仍然得到旧的结果,请你告诉我这是什么问题?

public class Adaptor extends ArrayAdapter<String> implements Filterable{

        private ArrayList<String> items;

        public Adaptor(Context context, int textViewResourceId, String[] objects) {
            super(context, textViewResourceId, objects);
            items = new ArrayList<String>();
            for (int i = 0; i < objects.length ; i++)
                items.add(objects[i]);

        }


        @Override
        public int getCount() {
            return items.size();
        }


        @Override
        public String getItem(int position) {
            return items.get(position);
        }

        @Override
        public Filter getFilter() {
            Filter myFilter = new Filter(){

                @Override
                protected FilterResults performFiltering(CharSequence arg0) {
                    FilterResults rezultate = new FilterResults();
                    ArrayList<String> chestii = new ArrayList<String>();
                    for (int i = 0; i < items.size() ; i++)
                    {
                        String tmp = items.get(i).toUpperCase();
                        if (tmp.startsWith(arg0.toString().toUpperCase()))
                                chestii.add(items.get(i));

                    }
                    rezultate.count = chestii.size();
                    rezultate.values = chestii;
                    return rezultate;
                }

                @Override
                protected void publishResults(CharSequence constraint,
                        FilterResults results) {
                    if (results != null && results.count > 0)
                    {
                        notifyDataSetChanged();
                    }
                    else notifyDataSetInvalidated();

                }

            };
            return myFilter;
        }




    }

1 个答案:

答案 0 :(得分:3)

您的列表包含“项目”中的项目,并且您不修改其中的项目,您必须从“项目”中删除位置,然后调用notifyDataSetChange,以便最近恢复您必须保存以前项目的所有项目

这很好用:

/**
 * 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;
            }
        };
    }

}