Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - cannot update listview with filtered results from edittext string
    primarykey
    data
    text
    <p>I'm experiencing some problems filtering contacts on the phone by the search string typed by the user in an edittext.</p> <p>It seems like it's filtering well, but the problem is that the filtered ListView contains the same initial contacts as the ALL CONTACTS ListView.</p> <p>For example:</p> <pre><code>ALL CONTACTS: A - B - C - D - E </code></pre> <p>I write E in the <code>EditText</code> and, using a <code>TextWatcher</code>, the filtered result is <code>A</code>.</p> <p>So it's like it's filtering well (only one result), but the listview content has not been updated!</p> <p>Some code here:</p> <p><code>getFilter()</code> and custom filter:</p> <pre><code>@Override public Filter getFilter() { 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("PREFIX", "its ---&gt; " + prefix); prefix = prefix.toString().toLowerCase(); if(prefix != null &amp;&amp; prefix.length()&gt;0) { ArrayList&lt;Contact&gt; contatti = new ArrayList&lt;Contact&gt;(); for(int i = 0; i &lt; _app._contacts.size(); i++) { Contact current = _app._contacts.get(i); if(current.getContactName().toLowerCase(Locale.getDefault()).contains(prefix) /*|| current.getNumber().toLowerCase().contains(prefix) || current.getEmail().toLowerCase().contains(prefix) || current.getOrganization().toLowerCase().contains(prefix)*/) { contatti.add(current); Log.d("added to contatti list: ", current.getContactName()+" - "+current.getNumber()); } } results.count = contatti.size(); results.values = contatti; } else { results.count = _app._contacts.size(); results.values = _app._contacts; } return results; } @SuppressWarnings("unchecked") @Override protected void publishResults(CharSequence constraint, FilterResults results) { ArrayList&lt;Contact&gt; risultatoFiltro = new ArrayList&lt;Contact&gt;(); risultatoFiltro = (ArrayList&lt;Contact&gt;) results.values; MyAdapter filteredAdapter = new MyAdapter(_ctx,R.layout.listview,risultatoFiltro); MainActivity.lv.setAdapter(filteredAdapter); notifyDataSetChanged(); } } </code></pre> <p><strong>EDIT</strong></p> <p>Edited publishResults method:</p> <pre><code>protected void publishResults(CharSequence constraint, FilterResults results) { if (results.count == 0) notifyDataSetInvalidated(); else { ArrayList&lt;Contact&gt; risultatoFiltro = (ArrayList&lt;Contact&gt;) results.values; notifyDataSetChanged(); clear(); //Add the items back in for (Iterator&lt;Contact&gt; iterator = risultatoFiltro.iterator(); iterator.hasNext();) { Contact contactList = (Contact) iterator.next(); add(contactList); } } } </code></pre> <p>Now the filtering works well, but if I delete some characters the list doesn't update!</p> <p>Example: write 'Daniel'. If I delete all except 'D' the list remains 'Daniel'! And if I delete the 'D' the list is not the initial one, but it gives me an empty list!</p> <p>Can you help me figuring this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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