Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Perhaps this may help you:</p> <pre><code>import java.util.ArrayList; import java.util.Arrays; import java.util.List; import android.content.Context; import android.util.Log; import android.widget.ArrayAdapter; import android.widget.Filter; public class CustomAdapter&lt;T&gt; extends ArrayAdapter&lt;T&gt; { private final ArrayList&lt;T&gt; mOriginalValues; private List&lt;T&gt; mObjects; private CustomFilter mFilter; private final Object mLock = new Object(); public CustomAdapter(Context context, int textViewResourceId, T[] objects) { super(context, textViewResourceId, objects); mObjects = Arrays.asList(objects); mOriginalValues = (ArrayList&lt;T&gt;) Arrays.asList(objects); // TODO Auto-generated constructor stub } @Override public Filter getFilter() { // TODO Auto-generated method stub if (mFilter == null) { mFilter = new CustomFilter(); } return mFilter; } private class CustomFilter extends Filter { @Override protected FilterResults performFiltering(CharSequence prefix) { FilterResults results = new FilterResults(); Log.d("bajji", "its ---&gt; " + prefix); if (prefix == null || prefix.toString().trim().length() == 0) { ArrayList&lt;T&gt; list; synchronized (mLock) { list = new ArrayList&lt;T&gt;(mOriginalValues); } results.values = list; results.count = list.size(); } else { String prefixString = prefix.toString().toLowerCase(); ArrayList&lt;T&gt; values; synchronized (mLock) { values = new ArrayList&lt;T&gt;(mOriginalValues); } final int count = values.size(); final ArrayList&lt;T&gt; newValues = new ArrayList&lt;T&gt;(); final ArrayList&lt;T&gt; approxValues = new ArrayList&lt;T&gt;(); final ArrayList&lt;T&gt; secondApproxValues = new ArrayList&lt;T&gt;(); for (int i = 0; i &lt; count; i++) { final T value = values.get(i); final String valueText = value.toString().toLowerCase(); boolean flag = true; // First match against the whole, non-splitted value if (valueText.startsWith(prefixString)) { newValues.add(value); flag = false; } else { final String[] words = valueText.split(" "); final int wordCount = words.length; // Start at index 0, in case valueText starts with space(s) for (int k = 0; k &lt; wordCount; k++) { if (words[k].startsWith(prefixString)) { newValues.add(value); flag = false; break; } } } if(flag) { if(approxMatch(valueText, prefixString) &lt;= 3) { //change the stuff and do a levi work approxValues.add(value); } else { final String[] words = valueText.split(" "); final int wordCount = words.length; // Start at index 0, in case valueText starts with space(s) for (int k = 0; k &lt; wordCount; k++) { if(approxMatch(words[k], prefixString) &lt;= 3) { //leve work secondApproxValues.add(value); break; } } } } } newValues.addAll(approxValues); newValues.addAll(secondApproxValues); results.values = newValues; results.count = newValues.size(); } return results; } @Override protected void publishResults(CharSequence constraint, FilterResults results) { //noinspection unchecked mObjects = (List&lt;T&gt;) results.values; notifyDataSetChanged(); clear(); for(T tmp : mObjects){ add(tmp); } notifyDataSetChanged(); } } private int approxMatch (String s, String t) { // an approxmimate string matching algo return p; } } </code></pre> <p>Now in <code>getView</code> method of adapter, you need to refer to <strong>mObjects</strong> object to get recent available values for ListView</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload