I have a ListView
and a EditText
. How can I filter ListView data when typing on EditText
?
views:
2340answers:
5
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
2009-10-29 18:30:42
What happens if someone over types then hits the backspace key?
fiXedd
2009-10-30 01:03:03
+5
A:
- Add
TextWatcher
toEditText#addTextChangedListener
- In
onTextChanged
add or remove items from yourListView
's adapter. If you are subclassingArrayAdapter
it would haveadd
andremove
methods
DroidIn.net
2009-10-29 18:31:34
+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:
- creating a new list adapter and populating it with the items that satisfy the filter or
- having a subclass of the
BaseAdapter
to accept your filter and callnotifyDataSetChanged()
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
2009-10-30 05:10:40
A:
Johe, Have a look here, explanation is VERY clear. http://stackoverflow.com/questions/1901020/android-xml-list-view-with-filter/2282012#2282012
Sephy
2010-02-17 16:02:10