自定义适配器过滤无效的AutoCompleteTextView

时间:2017-10-31 11:41:12

标签: android android-arrayadapter autocompletetextview custom-adapter android-filterable

我的android项目中有一个autocomplete text view,它工作正常,但只有在输入第一个值时才有效。因此,为了使其更具可定制性,我添加了以下类

public class CustomArrayAdapterWIthFilter extends ArrayAdapter<String> implements Filterable {
List<String> items = null;
List<String> originalItems = null;
private MyFilters myFilters = null;
Context mContext;

public CustomArrayAdapterWIthFilter(@NonNull Context context, @LayoutRes int resource, @NonNull List<String> objects) {
    super(context, resource, objects);
    this.items = new ArrayList<>();
    items.addAll(objects);
    this.originalItems = new ArrayList<>();
    this.originalItems.addAll(objects);
    mContext = context;
}

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

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

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

@Override
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
}

@NonNull
@Override
public Filter getFilter() {
    if (myFilters == null) {
        myFilters = new MyFilters();
    }
    return myFilters;
}

class MyFilters extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults result = new FilterResults();

        if (constraint != null && constraint.toString().length() > 0) {
            ArrayList<String> filteredItems = new ArrayList<String>();

            for (int i = 0, l = originalItems.size(); i < l; i++) {
                String item = originalItems.get(i);
                if (item.toString().toLowerCase().contains(constraint))
                    filteredItems.add(item);
            }
            result.count = filteredItems.size();
            result.values = filteredItems;
        } else {
            synchronized (this) {
                result.values = originalItems;
                result.count = originalItems.size();
            }
        }
        return result;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        items = (ArrayList<String>) results.values;
        notifyDataSetChanged();
        clear();
        for (int i = 0, l = items.size(); i < l; i++)
            add(items.get(i));
        notifyDataSetInvalidated();
    }
}

public void addItems(List<String> items) {
    this.items = items;
    notifyDataSetChanged();
}

在我的片段中,我做了以下

CustomArrayAdapterWIthFilter meterSrNumAdapter;
 if (meterSrNumAdapter.getCount() == 0) {
            meterSrArrayList = new MeterManager(getActivity()).getRefNoByStatus(Constants.SR_NO_UN_USED);
            meterSrArrayList.add(installationDetails.getMsn());
            meterSrNumAdapter.addItems(meterSrArrayList);
        }
 if (meterSrNumAdapter.getCount() == 0) {
            meterSrArrayList = new MeterManager(getActivity()).getRefNoByStatus(Constants.SR_NO_UN_USED);
            meterSrNumAdapter.addItems(meterSrArrayList);
        }

meterSrArrayList = new ArrayList<String>();
    meterSrNumAdapter = new CustomArrayAdapterWIthFilter(getActivity(), R.layout.custom_spinner_layout, meterSrArrayList);
    smartMsnSpinner.setAdapter(meterSrNumAdapter);
    smartMsnSpinner.setSingleLine(true);
    smartMsnSpinner.setThreshold(1);

    smartMsnSpinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
           selectedMeterNo = adapterView.getItemAtPosition(i).toString();
            Log.d("testtt", adapterView.getItemAtPosition(i).toString());
        }


    });

上面的代码smartMsnSpinner是我的自动填充文字视图

当我运行我的应用时,自动完成功能不起作用。我已经尝试了一些放在互联网上的其他适配器。但结果仍然相同。我正在尝试这个自定义适配器,因为在我的另一个应用程序中,它工作正常,在这段代码中我也是这样做的。

更新1

建议之后我更改了自定义适配器代码,同时考虑代码的其他部分与上面添加的相同

List<String> suggestionItems = null;

//In constructor
this.items = new ArrayList<>(objects);

this.originalItems = new ArrayList<>(objects);

mContext = context;

class MyFilters extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        if(constraint !=null)
        {
            suggestionItems.clear();

            for(int i=0, i1 = originalItems.size(); i <i1; i++)
            {
                String item = originalItems.get(i);
                if(item.toString().toLowerCase().contains(constraint))
                    suggestionItems.add(item);
            }
            FilterResults filterResults = new FilterResults();
            filterResults.values = suggestionItems;
            filterResults.count = suggestionItems.size();
            return filterResults;
        }
        else
        {
            return new FilterResults();
        }

    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
         if(results !=null && results.count > 0)
         {
             items.clear();
             items = (ArrayList<String>)results.values;
             notifyDataSetChanged();
         }
         else
         {
             items.addAll(originalItems);
         }
    }
}

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:-1)

在构造函数中不需要使用add all,你可以像这样分配并在适配器中再创建一个,

List<String> suggestionItems = null;

//constructor should be like this 

this.items = new ArrayList<>(objects);
this.originalItems = new ArrayList<>(objects);
this.suggestionItems = new ArrayList<>();

和过滤器应

class MyFilters extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        if (constraint != null)
        {
            suggestionItems .clear();
            for (int i = 0, l = originalItems.size(); i < l; i++) {
                String item = originalItems.get(i);
                if (item.toString().toLowerCase().contains(constraint))
                    suggestionItems .add(item);
            }
            FilterResults filterResults = new FilterResults();
            filterResults.values = suggestionItems ;
            filterResults.count = suggestionItems .size();
            return filterResults;
        }
        else
        {
            return new FilterResults();
        }
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        if (results != null && results.count > 0) {
            items.clear();
            items = (ArrayList<String>) results.values;
            notifyDataSetChanged();
        }
        else {
            items .addAll(originalItems );
        }
    }
}