如何用listview实现autocompletetextview?

如何用listview实现autocompletetextview?,第1张

如何用listview实现autocompletetextview?

首先创建一个实现可过滤的自定义适配器

public class MyFilterableAdapter extends baseAdapter implements Filterable {    private Context context;    private List<String> items;    private List<String> filteredItems;    private ItemFilter mFilter = new ItemFilter();    public MyFilterableAdapter(Context context, List<String> items) {        //super(context, R.layout.your_row, items);        this.context = context;        this.items = items;        this.filteredItems = items;    }    @Override    public int getCount() {        return filteredItems.size();    }    @Override    public Object getItem(int position) {        return filteredItems.get(position);    }    @Override    public long getItemId(int position) {        return position;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        ViewHolder viewHolder;        if (convertView == null) { LayoutInflater inflater = (LayoutInflater) context         .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.row_search_merchant, parent, false); viewHolder = new ViewHolder(); viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title); convertView.setTag(viewHolder);        } else { viewHolder = (ViewHolder) convertView.getTag();        }        String location = filteredItems.get(position);        if (!location.isEmpty() || viewHolder != null) { viewHolder.tvTitle.setText(location);        }        return convertView;    }    public static class ViewHolder {        TextView tvTitle;    }    private class ItemFilter extends Filter {        @Override        protected FilterResults performFiltering(CharSequence constraint) { String filterString = constraint.toString().toLowerCase(); FilterResults results = new FilterResults(); int count = items.size(); final List<String> tempItems = new ArrayList<>(count); for (int i = 0; i < count; i++) {     if (items.get(i).toLowerCase().contains(filterString)) {         tempItems.add(items.get(i));     } } results.values = tempItems; results.count = tempItems.size(); return results;        }        @SuppressWarnings("unchecked")        @Override        protected void publishResults(CharSequence constraint, FilterResults results) { filteredItems = (ArrayList<String>) results.values; notifyDataSetChanged();        }    }    public Filter getFilter() {        return mFilter;    }}

而不是创建一个文本监视器

public class MyTextWatcher implements TextWatcher {    private MyCustomAdapter lAdapter;    public MyTextWatcher(MyCustomAdapter lAdapter) {        this.lAdapter = lAdapter;    }    @Override    public void beforeTextChanged(CharSequence s, int start, int count, int after) {    }    @Override    public void onTextChanged(CharSequence s, int start, int before, int count) {    }    @Override    public void afterTextChanged(Editable s) {        lAdapter.getFilter().filter(s.toString().toLowerCase());    }}

最后将您的TextWatcher添加到您的搜索编辑文本中:

final MyCustomAdapter lAdapter = new MyCustomAdapter(context, items);EditText etSearch = (EditText) view.findViewById(R.id.et_search);        etSearch.addTextChangedListener(new SearchTextWatcher(lAdapter));

希望对您有帮助。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5477522.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存