tags:

views:

2340

answers:

5

I have a ListView and a EditText. How can I filter ListView data when typing on EditText ?

A: 

1) create a custom adapter for your list view and create a removeIfMatch(String s) method:

public void removeIfMatch(String s) {
  for item in adapter:
    if item.matches(s) {
       data.removeItem(item);
       notifyDataSetChanged();
       break
    }
}

2) create a callback for when the EditText contents change

3) invoke adapter.removeIfMatch(editText.getText())

Matthias
What happens if someone over types then hits the backspace key?
fiXedd
+5  A: 
  1. Add TextWatcher to EditText#addTextChangedListener
  2. In onTextChanged add or remove items from your ListView's adapter. If you are subclassing ArrayAdapter it would have add and remove methods
DroidIn.net
It's work! Thanks!
Dennie
+4  A: 

You can use:

http://developer.android.com/reference/android/widget/TextView.html

addTextChangedListener( TextWatcher watcher )

to figure out when the textview was changed. I believe it should be called everytime a letter is added or removed.

Then update your list adapter to dislplay the new items by either:

  1. creating a new list adapter and populating it with the items that satisfy the filter or
  2. having a subclass of the BaseAdapter to accept your filter and call notifyDataSetChanged() after it has finished removing the items you no longer want

http://developer.android.com/reference/android/widget/BaseAdapter.html

So you mean for the TextView? I'm using EditText. Anyway, I think it's also useful for me! thanks!
Dennie
Sorry. Fortunately, EditText is a subclass of TextView and was still relevant. I'm glad it helped.
+1  A: 

Think my solution to this might be just what you are looking for!

http://stackoverflow.com/questions/1737009/answer-to-making-a-nice-looking-listview-filter-on-android/1737042#1737042

Hamy
A: 

Johe, Have a look here, explanation is VERY clear. http://stackoverflow.com/questions/1901020/android-xml-list-view-with-filter/2282012#2282012

Sephy