A java FilterArrayList implementation

Source: Internet
Author: User

For Android projects, multi-field fuzzy filtering is required for a ListView. The ArrayAdapter provided by the system provides the filtering function, but the project uses the BaseAdapter adapter and List <Object> as the data source, if you want to implement filtering, You need to traverse the original List and generate a new List. At least two lists are required. Therefore, you want to implement filtering without generating a new List. first declare a filter interface IObjectFilter public interface IObjectFilter {<br> // if the Object matches successfully, return true boolean filter (object Object object);} and inherit from ArrayList to implement FilterArrayList: public class FilterArrayList <T> extends ArrayList <T> {/*****/private static fin Al long serialVersionUID = 1975425155027160540l; private IObjectFilter mFilter; private Integer [] mFilteredIndices; // filtered index, public FilterArrayList (List <T> list) {super (list );} public FilterArrayList () {super ();} public boolean add (T object) {<br> // if the current list is filtered, you cannot add a new object if (mFilter! = Null) {return false;} return super. add (object) ;}/ * <br> * When filtering, you need to complete a series of insert/Delete actions. <br> */@ Override public int size () {if (mFilteredIndices! = Null) {<br> // the size of the filtered index set is the size of the entire List. return mFilteredIndices. length;} else {return super. size () ;}} public T get (int index) {if (mFilteredIndices! = Null) {<br> // The index = getFilteredIndex (index);} return super. get (index) ;}public void setFilter (IObjectFilter filter) {if (filter = null) {removeFilter () ;}else {mFilter = filter; applyFilter ();}} private void applyFilter () {mFilteredIndices = null; int size = super. size (); List <Integer> indices = new ArrayList <Integer> (); for (int I = 0; I <size; I ++) {<br> // call the filter interface if (mFilter. filter (super. get (I) {<br> // if true is returned, the index indices is saved. add (I) ;}} mFilteredIndices = new Integer [indices. size ()]; indices. toArray (mFilteredIndices);} public void removeFilter () {mFilter = null; mFilteredIndices = null;} private int getFilteredIndex (int index) {return mFilteredIndices [index];} usage: // entity class Employee {String empNo; String briefCode; String name ;...}; list <Employee> employees = new List <Employee> (); employees. add (...); employees. add (...); employees. add (...);..... // The following shows how to filter String filterString; // obtain the fuzzy match String on the Interface // instantiate the filter IObjectFilter filter = new IObjectFilter () {public boolean filter (Object object Object) {Employee emp = (Employee) object; return (emp. empNo. indexOf (filterString)>-1 | emp. name. indexOf (filterString)>-1 | emp. briefCode. indexOf (filterString)>-1) }}; // sets the filter employees for the List. setFilter (filter); // OK. Now the List is the object to be filtered out. <br> // cancel filtering <br> employees. removeFilter (); <br> or <br> employees. setFilter (null); in fact, this method does not change the data of the original array, but only exchanges the location of the index to avoid repeated listing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.