Example usage for android.widget Filter Filter

List of usage examples for android.widget Filter Filter

Introduction

In this page you can find the example usage for android.widget Filter Filter.

Prototype

public Filter() 

Source Link

Document

Creates a new asynchronous filter.

Usage

From source file:se.chalmers.watchme.activity.AutoCompleteAdapter.java

@Override
public Filter getFilter() {

    return new Filter() {

        /*/* w  w  w .j  a  v a  2  s.  c om*/
         * Override this method in order to populate the auto complete field
         * with the actual movie name when selecting a suggestion from the list,
         * instead of the default toString implementation.
         * 
         * @see android.widget.Filter#convertResultToString(java.lang.Object)
         */
        @Override
        public CharSequence convertResultToString(Object result) {
            if (result instanceof JSONObject) {
                return ((JSONObject) result).optString(Movie.JSON_KEY_NAME);
            }

            return super.convertResultToString(result);
        }

        /*
         * The actual filtering, where the call to the API is made. The method
         * is called in a worker thread apart from the UI thread, so we're okay.
         * 
         * @see android.widget.Filter#performFiltering(java.lang.CharSequence)
         */
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            FilterResults results = new FilterResults();
            if (constraint != null) {
                JSONArray json = source.getMoviesByTitle(constraint.toString());

                results.values = json;
                results.count = json.length();
            }

            return results;
        }

        /*
         * Update the list.
         * 
         * @see android.widget.Filter#publishResults(java.lang.CharSequence, android.widget.Filter.FilterResults)
         */
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            if (results != null && results.count > 0) {
                movies = (JSONArray) results.values;
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }

    };
}

From source file:com.capstone.transit.trans_it.PlacesAutoCompleteAdapter.java

@Override
public Filter getFilter() {
    Filter filter = new Filter() {
        @Override//from w  ww.  j a  va 2  s  .c om
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults filterResults = new FilterResults();
            if (constraint != null) {
                // Retrieve the autocomplete results.
                resultList = autocomplete(constraint.toString());

                // Assign the data to the FilterResults
                filterResults.values = resultList;
                filterResults.count = resultList.size();
            }
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            if (results != null && results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    };
    return filter;
}

From source file:ch.ethz.tik.hrouting.providers.PlacesAutoCompleteAdapter.java

@Override
public Filter getFilter() {
    return new Filter() {
        @Override//w  w w . j  av  a2s .c  o  m
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults filterResults = new FilterResults();
            if (constraint != null) {
                // Retrieve the auto-complete results.
                resultList = autocomplete(constraint.toString());

                // Assign the data to the FilterResults
                if (resultList != null) {
                    filterResults.values = resultList;
                    filterResults.count = resultList.size();
                }
            }
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            if (results != null && results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    };
}

From source file:tw.geodoer.mGeoInfo.controller.PlaceAutocompleteAdapter.java

/**
 * Returns the filter for the current set of autocomplete results.
 *///  w ww .java  2s.  co m
@Override
public Filter getFilter() {
    Filter filter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            // Skip the autocomplete query if no constraints are given.
            if (constraint != null) {
                // Query the autocomplete API for the (constraint) search string.
                mResultList = getAutocomplete(constraint);
                if (mResultList != null) {
                    // The API successfully returned results.
                    results.values = mResultList;
                    results.count = mResultList.size();
                }
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            if (results != null && results.count > 0) {
                // The API returned at least one result, update the data.
                notifyDataSetChanged();
            } else {
                // The API did not return any results, invalidate the data set.
                notifyDataSetInvalidated();
            }
        }
    };
    return filter;
}

From source file:com.justplay1.shoppist.features.search.SearchAdapter.java

@Override
public Filter getFilter() {
    return new Filter() {
        @Override/*from w ww .  j a v a  2s  .  c o m*/
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            if (constraint == null || constraint.length() == 0) {
                results.values = data.values();
                results.count = data.size();
            } else {
                String prefixString = constraint.toString().toLowerCase();
                final List<ProductViewModel> newValues = new ArrayList<>();

                if (!data.containsKey(prefixString)) {
                    ProductViewModel product = new ProductViewModel();
                    product.setId(SearchView.JUST_NAME);
                    product.setName(constraint.toString());
                    UnitViewModel unit = new UnitViewModel();
                    unit.setId(UnitViewModel.NO_UNIT_ID);
                    product.setUnit(unit);
                    CategoryViewModel category = new CategoryViewModel();
                    category.setId(CategoryViewModel.NO_CATEGORY_ID);
                    product.setCategory(category);
                    newValues.add(product);
                }

                for (final ProductViewModel value : data.values()) {
                    final String valueText = value.getName().toLowerCase();

                    if (valueText.startsWith(prefixString)) {
                        newValues.add(value);
                    } else {
                        final String[] words = valueText.split(" ");

                        // Start at index 0, in case valueText starts with space(s)
                        for (String word : words) {
                            if (word.startsWith(prefixString)) {
                                int start = valueText.length() - word.length();
                                newValues.add(value);
                                break;
                            }
                        }
                    }
                }
                results.values = newValues;
                results.count = newValues.size();
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, Filter.FilterResults results) {
            if (constraint == null)
                return;
            if (results.values == null)
                return;

            itemsToDisplay.clear();
            itemsToDisplay.addAll((Collection<ProductViewModel>) results.values);
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyItemRangeRemoved(0, getItemCount());
            }
        }
    };
}

From source file:com.ccjeng.weather.view.adapter.PlaceAutocompleteAdapter.java

/**
 * Returns the filter for the current set of autocomplete results.
 *//*from   ww  w  . j  a  v  a 2  s  . c om*/
@Override
public Filter getFilter() {
    return new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();

            // We need a separate list to store the results, since
            // this is run asynchronously.
            ArrayList<AutocompletePrediction> filterData = new ArrayList<>();

            // Skip the autocomplete query if no constraints are given.
            if (constraint != null) {
                // Query the autocomplete API for the (constraint) search string.
                filterData = getAutocomplete(constraint);
            }

            results.values = filterData;
            if (filterData != null) {
                results.count = filterData.size();
            } else {
                results.count = 0;
            }

            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {

            if (results != null && results.count > 0) {
                // The API returned at least one result, update the data.
                mResultList = (ArrayList<AutocompletePrediction>) results.values;
                notifyDataSetChanged();
            } else {
                // The API did not return any results, invalidate the data set.
                notifyDataSetInvalidated();
            }
        }

        @Override
        public CharSequence convertResultToString(Object resultValue) {
            // Override this method to display a readable result in the AutocompleteTextView
            // when clicked.
            if (resultValue instanceof AutocompletePrediction) {
                return ((AutocompletePrediction) resultValue).getFullText(null);
            } else {
                return super.convertResultToString(resultValue);
            }
        }
    };
}

From source file:com.tmendes.birthdaydroid.BirthDayArrayAdapter.java

@Override
public Filter getFilter() {
    return new Filter() {

        @SuppressWarnings("unchecked")
        @Override/*from   www.j a v  a 2s  .c o m*/
        protected void publishResults(CharSequence constraint, Filter.FilterResults results) {
            contactsBirthDays = (ArrayList<ContactData>) results.values;
            notifyDataSetChanged();
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            ArrayList<ContactData> FilteredArrList = new ArrayList<>();

            if (constraint == null || constraint.length() == 0) {
                results.count = orglBDList.size();
                results.values = orglBDList;
            } else {
                constraint = constraint.toString().toLowerCase();
                for (int i = 0; i < orglBDList.size(); i++) {
                    String data = orglBDList.get(i).getName();
                    if (data.toLowerCase().startsWith(constraint.toString())) {
                        ContactData contact = new ContactData(ctx, orglBDList.get(i).getKey(),
                                orglBDList.get(i).getName(), orglBDList.get(i).getDate(),
                                orglBDList.get(i).getPhotoURI());
                        FilteredArrList.add(contact);
                    }
                }
                results.count = FilteredArrList.size();
                results.values = FilteredArrList;
            }

            return results;
        }
    };
}

From source file:com.moto.miletus.application.DeviceListAdapter.java

@Override
public Filter getFilter() {
    return new Filter() {
        @Override/*from ww w  . j a v  a 2 s  .  c o  m*/
        protected FilterResults performFiltering(final CharSequence constraint) {
            final FilterResults results = new FilterResults();
            final List<DeviceWrapper> dataSetFilter = new ArrayList<>();

            if (constraint.length() == 0) {
                dataSetFilter.addAll(getDataSetOriginal());
            } else {
                for (final DeviceWrapper device : getDataSetOriginal()) {
                    if (StringUtils.containsIgnoreCase(device.getDevice().getName(), constraint.toString())) {
                        dataSetFilter.add(device);
                    }
                }
            }

            results.values = dataSetFilter;
            results.count = dataSetFilter.size();
            return results;
        }

        @Override
        protected void publishResults(final CharSequence constraint, final FilterResults results) {
            if (results.values instanceof List) {
                getDataSetFilter().clear();
                setDataSetFilter((List<DeviceWrapper>) results.values);
                sort();
            }
        }
    };
}

From source file:eu.faircode.netguard.AdapterRule.java

@Override
public Filter getFilter() {
    return new Filter() {
        @Override/*from   w  w w.  j  av a 2 s.  co m*/
        protected FilterResults performFiltering(CharSequence query) {
            List<Rule> listResult = new ArrayList<>();
            if (query == null)
                listResult.addAll(listAll);
            else {
                query = query.toString().toLowerCase().trim();
                int uid;
                try {
                    uid = Integer.parseInt(query.toString());
                } catch (NumberFormatException ignore) {
                    uid = -1;
                }
                for (Rule rule : listAll)
                    if (rule.info.applicationInfo.uid == uid
                            || rule.info.packageName.toLowerCase().contains(query)
                            || (rule.name != null && rule.name.toLowerCase().contains(query)))
                        listResult.add(rule);
            }

            FilterResults result = new FilterResults();
            result.values = listResult;
            result.count = listResult.size();
            return result;
        }

        @Override
        protected void publishResults(CharSequence query, FilterResults result) {
            listFiltered.clear();
            if (result == null)
                listFiltered.addAll(listAll);
            else {
                listFiltered.addAll((List<Rule>) result.values);
                if (listFiltered.size() == 1)
                    listFiltered.get(0).expanded = true;
            }
            notifyDataSetChanged();
        }
    };
}