Note that there are some explanatory texts on larger screens.

plurals
  1. POshow full list when EditText box is empty
    primarykey
    data
    text
    <p>I have an EditText component that searches a list using a custom adapter. It works fine except that the filtered views remain on screen. If the user deletes all the text in the edit box, I would like the full list to return to the screen. </p> <p>I've tried three approaches:</p> <ol> <li><p>I put a condition in my adapter's filter classs. Placeing <code>notifyDataSetChanged();</code> in FilterResults function, and it crashes the program.</p></li> <li><p>Detecting if the EditTtext box is empty in the Activity, and then calling <code>notifyDataSetChanged();</code>. The condition works because I see <code>System.out.println()</code> prints text to the console. but it has no effect on the listView. Only the filtered text remains on screen.</p></li> <li><p>Placing <code>notifyDataSetChanged();</code> in <code>OnTextChanged()</code>. This also has no effect on resetting the listview. </p></li> </ol> <p>How do I get the listview to reset when the EditText Box is empty? </p> <p>Code from Activity:</p> <p>// in OnCreate</p> <pre><code>if (searchBox.toString().equals("")){ objAdapter.notifyDataSetChanged(); } searchBox.addTextChangedListener(new TextWatcher(){ @Override public void afterTextChanged(Editable arg0) { // Do nothing } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // Do nothing } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { objAdapter.getFilter().filter(s); if (s.toString().equals("")){ // objAdapter.notifyDataSetChanged(); fails here } } } }); </code></pre> <p>Code from Adapter. </p> <pre><code> public class NewsRowAdapter extends ArrayAdapter&lt;Item&gt; { //.... code private class ListFilter extends Filter { @Override protected FilterResults performFiltering(CharSequence constraint) { FilterResults results = new FilterResults(); String prefix = constraint.toString().toLowerCase(); if (prefix == null || prefix.length() == 0) { System.out.println("editText is empty"); // notifyDataSetChanged(); no effect ArrayList&lt;Item&gt; list = new ArrayList&lt;Item&gt;(original); results.values = list; results.count = list.size(); } else { final ArrayList&lt;Item&gt; list = original; int count = list.size(); final ArrayList&lt;Item&gt; nlist = new ArrayList&lt;Item&gt;(count); for (int i=0; i&lt;count; i++) { final Item pkmn = list.get(i); final String value = pkmn.getName().toLowerCase(); if (value.startsWith(prefix)) { nlist.add(pkmn); } } results.values = nlist; results.count = nlist.size(); } return results; } @SuppressWarnings("unchecked") @Override protected void publishResults(CharSequence constraint, FilterResults results) { fitems = (ArrayList&lt;Item&gt;)results.values; clear(); int count = fitems.size(); for (int i=0; i&lt;count; i++) { Item pkmn = (Item)fitems.get(i); add(pkmn); } if (fitems.size() &gt; 0){ notifyDataSetChanged(); } else{ notifyDataSetInvalidated(); } } } </code></pre>
    singulars
    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.
    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