I want to search a ListView when user types a text in a EditText. after pressing a key I need to get the typed part of text to begin the search. so is there any method I can use?? simple help is highly appreciated.
Asked
Active
Viewed 3,518 times
4 Answers
1
If you're using an ArrayAdapter
to populate your ListView
, then do it like this:
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
yourAdapter.getFilter().filter(s);
}
@Override
public void afterTextChanged(Editable s) {
}
});
Add a TextWatch
to your EditText
and user the filter
in your adapter to show the results that match. To highlight the matched result , you may want to use SpannableString for "prettyfying" it.
From ArrayAdapter.getFilter():
Returns a filter that can be used to constrain data with a filtering pattern.
This method is usually implemented by Adapter classes.
0
you can implement TextWatcher in your activity that a look at:
http://developer.android.com/reference/android/text/TextWatcher.html

Adrian Cid Almaguer
- 7,815
- 13
- 41
- 63
0
Let's say you have a view xml
<EditText
android:id="@+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:imeOptions="actionSend" />
Your code in the controller will be
final EditText searchField = (ListView)result.findViewById(R.id.search);;
searchField.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
//do search is the method where you will delegate the search process.
doSearch(searchField.getText().toString());
}
});

Groovy Ed
- 307
- 2
- 9
-1
You have to set a TextWatcher to your edittext
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String newText = s.toString();
}
});

Elodie E2
- 580
- 3
- 10