Android search list while typing

后端 未结 5 1003
名媛妹妹
名媛妹妹 2020-12-04 15:06

How can I build a search bar where while I\'m typing the results are shown in the ListView in which I\'m searching?

For example, I have a list view with

5条回答
  •  广开言路
    2020-12-04 15:16

    Use following code to implement search and filter list in android:

    SearchAndFilterList.java

    public class SearchAndFilterList extends Activity {
    
        private ListView mSearchNFilterLv;
    
        private EditText mSearchEdt;
    
        private ArrayList mStringList;
    
        private ValueAdapter valueAdapter;
    
        private TextWatcher mSearchTw;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.activity_search_and_filter_list);
    
            initUI();
    
            initData();
    
            valueAdapter=new ValueAdapter(mStringList,this);
    
            mSearchNFilterLv.setAdapter(valueAdapter);
    
            mSearchEdt.addTextChangedListener(mSearchTw);
    
    
        }
        private void initData() {
    
            mStringList=new ArrayList();
    
            mStringList.add("one");
    
            mStringList.add("two");
    
            mStringList.add("three");
    
            mStringList.add("four");
    
            mStringList.add("five");
    
            mStringList.add("six");
    
            mStringList.add("seven");
    
            mStringList.add("eight");
    
            mStringList.add("nine");
    
            mStringList.add("ten");
    
            mStringList.add("eleven");
    
            mStringList.add("twelve");
    
            mStringList.add("thirteen");
    
            mStringList.add("fourteen");
    
            mSearchTw=new TextWatcher() {
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
    
                    valueAdapter.getFilter().filter(s);
                }
    
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
    
                }
    
                @Override
                public void afterTextChanged(Editable s) {
    
                }
            };
    
        }
    
        private void initUI() {
    
            mSearchNFilterLv=(ListView) findViewById(R.id.list_view);
    
            mSearchEdt=(EditText) findViewById(R.id.txt_search);
        }
    
    }
    

    Custom Value adapter: ValueAdapter.java

    public class ValueAdapter extends BaseAdapter implements Filterable{
    
        private ArrayList mStringList;
    
        private ArrayList mStringFilterList;
    
        private LayoutInflater mInflater;
    
        private ValueFilter valueFilter;
    
        public ValueAdapter(ArrayList mStringList,Context context) {
    
            this.mStringList=mStringList;
    
            this.mStringFilterList=mStringList;
    
            mInflater=LayoutInflater.from(context);
    
            getFilter();
        }
    
        //How many items are in the data set represented by this Adapter.
        @Override
        public int getCount() {
    
            return mStringList.size();
        }
    
        //Get the data item associated with the specified position in the data set.
        @Override
        public Object getItem(int position) {
    
            return mStringList.get(position);
        }
    
        //Get the row id associated with the specified position in the list.
        @Override
        public long getItemId(int position) {
    
            return position;
        }
    
        //Get a View that displays the data at the specified position in the data set.
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            Holder viewHolder;
    
            if(convertView==null) {
    
                viewHolder=new Holder();
    
                convertView=mInflater.inflate(R.layout.list_item,null);
    
                viewHolder.nameTv=(TextView)convertView.findViewById(R.id.txt_listitem);
    
                convertView.setTag(viewHolder);
    
            }else{
    
                viewHolder=(Holder)convertView.getTag();
            }
    
                viewHolder.nameTv.setText(mStringList.get(position).toString());
    
                return convertView;
        }
    
        private class  Holder{
    
            TextView nameTv;
        }
    
        //Returns a filter that can be used to constrain data with a filtering pattern.
        @Override
        public Filter getFilter() {
    
            if(valueFilter==null) {
    
                valueFilter=new ValueFilter();
            }
    
            return valueFilter;
        }
    
    
        private class ValueFilter extends Filter {
    
    
            //Invoked in a worker thread to filter the data according to the constraint.
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
    
                FilterResults results=new FilterResults();
    
                if(constraint!=null && constraint.length()>0){
    
                    ArrayList filterList=new ArrayList();
    
                    for(int i=0;i) results.values;
    
                notifyDataSetChanged();
    
    
            }
    
        }
    
    }
    

    activity_search_and_filter_list.xml

    
    
        
        
    
    
    

    list_item.xml

    
    
        
    
    
    

    AndroidManifext.xml

    
    
        
    
        
            
                
                    
    
                    
                
            
        
    
    
    

    I hope this code will will helpful to implement custom search and filter listview

提交回复
热议问题