How to implement autocompletetextview with listview?

前端 未结 1 880
时光说笑
时光说笑 2020-11-28 14:48

I am getting response from server and display it using listview and it works fine,now what I am trying is i added autocompletetextview to search items by name,but when i run

相关标签:
1条回答
  • 2020-11-28 15:31

    First create a custom adapter implements filterable:

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

    And than create a textwatcher

    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());
        }
    }
    

    And finally add your TextWatcher to your Search Edit Text:

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

    I hope this'll help you.

    0 讨论(0)
提交回复
热议问题