Note that there are some explanatory texts on larger screens.

plurals
  1. POSearch in a ListView doesn't work properly
    text
    copied!<p>I have a listView with about 30 items in it and have added a search function to it via an editText. When I type 'a' in the textfield everything that starts with an 'a' shows up on the list but when I type another letter the list disappears even tho the list contains an item with 'a' + the other letter i typed. </p> <p>Another thing that confuses me is that there is an item in the list called IWU Trading and thats the only item I can search for, means that if I type 'I' + 'W' the item shows up in the listView. But if I type 'a' + 'r' an item that is named 'art' dosent show up.</p> <p>My question(s). How can I do to make this search function work? Why does it act like it does?</p> <p>My XML</p> <pre><code> &lt;EditText android:id="@+id/searchEditText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/selectCustomerTextView" android:layout_marginTop="20dp" android:hint="@string/typeToSearch" android:ems="10" android:imeOptions="actionDone" android:inputType="textNoSuggestions"/&gt; &lt;ListView android:id="@+id/list_view" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@+id/nextButton" android:layout_alignParentLeft="true" android:layout_below="@+id/searchEditText" android:choiceMode="singleChoice" android:dividerHeight="5dp" &gt; &lt;/ListView&gt; </code></pre> <p>My code:</p> <pre><code> private ArrayAdapter&lt;Customer&gt; _oldAdapter = null; public void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); setContentView(R.layout.activity_customer_pick); EditText searchText = (EditText) findViewById(R.id.searchEditText); ListView listView = (ListView) findViewById(R.id.list_view); listView.setClickable(true); searchText.addTextChangedListener(filterTextWatcher); _oldAdapter = _phoneDAL.BindValues(this); listView.setAdapter(_oldAdapter); listView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) { String s = _oldAdapter.getItem(arg2).toString(); _listViewPostion = arg2; Toast.makeText(CustomerPick.this, "Du valde: " + s, arg2).show(); } }); } </code></pre> <p>The search method:</p> <pre><code>private TextWatcher filterTextWatcher = new TextWatcher() { public void afterTextChanged(Editable s) { } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { ArrayAdapter&lt;Customer&gt; _adapter = new ArrayAdapter&lt;Customer&gt;(CustomerPick.this, android.R.layout.simple_list_item_single_choice, new ArrayList&lt;Customer&gt;()); ListView listView = (ListView) findViewById(R.id.list_view); int textLength = s.length(); if(textLength == 0){ listView.setAdapter(_oldAdapter); return; } for (int i = 0; i &lt; listView.getAdapter().getCount(); i++) { String word = _oldAdapter.getItem(i).getCustName().toLowerCase(); if (textLength &lt;= word.length()) { if(s.toString().equalsIgnoreCase(word.substring(0, textLength))) { _adapter.add(_oldAdapter.getItem(i)); } } } listView.setAdapter(_adapter); } }; </code></pre> <p>}</p> <p>My CustomerClass (private ArrayAdapter _oldAdapter = null;)</p> <pre><code>public class Customer { private final int _custNumber; private final String _custName; public Customer(int custNumber, String custName){ _custNumber = custNumber; _custName = custName; } public int getCustNumber() { return _custNumber; } public String getCustName() { return _custName; } @Override public String toString(){ return _custName; } </code></pre> <p>}</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