List View Filter Android

前端 未结 3 2031
清歌不尽
清歌不尽 2020-11-22 14:09

I have created a list view in android and I want to add edit text above the list and when the user enter text the list will be filtered according to user input

can a

3条回答
  •  耶瑟儿~
    2020-11-22 14:41

    In case anyone are still interested in this subject, I find that the best approach for filtering lists is to create a generic Filter class and use it with some base reflection/generics techniques contained in the Java old school SDK package. Here's what I did:

    public class GenericListFilter extends Filter {
    
        /**
         * Copycat constructor
         * @param list  the original list to be used
         */
        public GenericListFilter (List list, String reflectMethodName, ArrayAdapter adapter) {
            super ();
    
            mInternalList = new ArrayList<>(list);
            mAdapterUsed  = adapter;
    
            try {
                ParameterizedType stringListType = (ParameterizedType)
                        getClass().getField("mInternalList").getGenericType();
                mCompairMethod =
                        stringListType.getActualTypeArguments()[0].getClass().getMethod(reflectMethodName);
            }
            catch (Exception ex) {
                Log.w("GenericListFilter", ex.getMessage(), ex);
    
                try {
                    if (mInternalList.size() > 0) {
                        T type = mInternalList.get(0);
                        mCompairMethod = type.getClass().getMethod(reflectMethodName);
                    }
                }
                catch (Exception e) {
                    Log.e("GenericListFilter", e.getMessage(), e);
                }
    
            }
        }
    
        /**
         * Let's filter the data with the given constraint
         * @param constraint
         * @return
         */
        @Override protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            List filteredContents = new ArrayList<>();
    
            if ( constraint.length() > 0 ) {
                try {
                    for (T obj : mInternalList) {
                        String result = (String) mCompairMethod.invoke(obj);
                        if (result.toLowerCase().startsWith(constraint.toString().toLowerCase())) {
                            filteredContents.add(obj);
                        }
                    }
                }
                catch (Exception ex) {
                    Log.e("GenericListFilter", ex.getMessage(), ex);
                }
            }
            else {
                filteredContents.addAll(mInternalList);
            }
    
            results.values = filteredContents;
            results.count  = filteredContents.size();
            return results;
        }
    
        /**
         * Publish the filtering adapter list
         * @param constraint
         * @param results
         */
        @Override protected void publishResults(CharSequence constraint, FilterResults results) {
            mAdapterUsed.clear();
            mAdapterUsed.addAll((List) results.values);
    
            if ( results.count == 0 ) {
                mAdapterUsed.notifyDataSetInvalidated();
            }
            else {
                mAdapterUsed.notifyDataSetChanged();
            }
        }
    
        // class properties
        private ArrayAdapter mAdapterUsed;
        private List mInternalList;
        private Method  mCompairMethod;
    }
    

    And afterwards, the only thing you need to do is to create the filter as a member class (possibly within the View's "onCreate") passing your adapter reference, your list, and the method to be called for filtering:

    this.mFilter = new GenericFilter (list, "getName", adapter);
    

    The only thing missing now, is to override the "getFilter" method in the adapter class:

    @Override public Filter getFilter () {
         return MyViewClass.this.mFilter;
    }
    

    All done! You should successfully filter your list - Of course, you should also implement your filter algorithm the best way that describes your need, the code bellow is just an example.. Hope it helped, take care.

提交回复
热议问题