Android自动完成功能,自定义过滤器具有重复结果

时间:2014-12-01 14:14:49

标签: android autocompletetextview android-filterable

美好的一天,我有这个自定义适配器,实现了可过滤的接口,并在结果列表中得到重复的值。

SearchAutoCompleteAdapter.java

public class SearchAutoCompleteAdapter extends BaseAdapter implements Filterable {

    private ArrayList<BaseAutocompleteItems> resultList;
    List<BaseAutocompleteItems> filteredProducts;
    private LayoutInflater layoutInflater;
    private Context context;
    private int layout;
    SearchAutoCompleteAPI searchautocomplete = new SearchAutoCompleteAPI();

    public SearchAutoCompleteAdapter(Context context, int resource) {
        super();
        this.context = context;
        this.layout = resource;
        filteredProducts = new ArrayList<BaseAutocompleteItems>();
        resultList = new ArrayList<BaseAutocompleteItems>();
    }


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


    @Override
    public Object getItem(int index) {
        return resultList.get(index);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(layout, null);

        }

        TextView name = (TextView) convertView.findViewById(R.id.suggestion_text_id);

        name.setText(resultList.get(position).getName());


        return convertView;
    }

    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {


                FilterResults filterResults = new FilterResults();
                List<BaseAutocompleteItems> tempfilteredProducts = new ArrayList<BaseAutocompleteItems
                filteredProducts.clear();

                if (constraint != null || constraint.length() > 0) {
                    tempfilteredProducts.clear();

                    tempfilteredProducts = searchautocomplete.autocomplete(constraint.toString());  //webservice call
                } else {

                    tempfilteredProducts = new ArrayList<BaseAutocompleteItems>();

                }

                    for (BaseAutocompleteItems items : tempfilteredProducts) {

                        if (items.getName().contains(constraint.toString())) {

                            filteredProducts.add(items);
                             }
                        }

                        filterResults.values = filteredProducts;
                        filterResults.count = filteredProducts.size();

                    return filterResults;
                }

                @Override
                protected void publishResults (CharSequence constraint, FilterResults results){

                    resultList = (ArrayList<BaseAutocompleteItems>)results.values;
                    if(results.count > 0)  {

                        notifyDataSetChanged();
                    } else {

                        notifyDataSetInvalidated();
                    }
                }
            }

            ;
            return filter;
        }

    }

如果我输入&#34;叫喊&#34;然后按退格键退出&#34; yel&#34;或者将我的字符增加到&#34; yello&#34;,我得到相同的结果,因此ArrayList最终得到重复的项目。我已经尝试在填充列表之前清除列表,但似乎没有任何效果。

2 个答案:

答案 0 :(得分:0)

尝试更改

if (items.getName().contains(constraint.toString()))

if (items.getName().startsWith(constraint.toString()))

答案 1 :(得分:0)

问题中的代码没有错。首先是任何人的清单。确保在行中返回的ArrayList上调用Clear()。

tempfilteredProducts = searchautocomplete.autocomplete(constraint.toString());  //webservice call
在从Web服务填充值并将其发送回tempfilteredProducts之前(即在每个api请求之前),首先从API调用

调用。这样就可以避免autocompletetextview字符串中的重复值,就像我在问题中的情况一样。