views:

1148

answers:

3

How can i build a search bar where while i'm typing the results are shown in the list view in which i'm searching?

For example, i have a list view with 20 strings. I press the search key and appears the bar. I want when i type 3 words or more the search starts running showing the results in the list view (as a filter: only shows the strings in the list that matching what i type)

+1  A: 

The best way is to use the built in search bar or SearchManager by overriding onSearchRequested in a searchable activity. You can set a datasource to search on to get the automatic drop down of results or you can just take in the input from the user and search after. Here is a good overview of SearchManager is a Plus there is a working demo in the API Demos project com.example.android.apis.app.SearchQueryResult

@Override
public boolean onSearchRequested() {
sadboy
yes.. but i don't want to get an automatic drop down results. I want the list where i'm seaching automatic refresh with the results of the search while i'm typing, not appears a drop down menu. Thanks!
xger86x
ahh, sorry i misunderstood the question. Without building your own custom view and trying to jump to listitems on keypress events I don't know of anything better off hand.
sadboy
+1  A: 

You can't do this with the search bar. But the listview has a possibility to filter on key pressed, like it is done in the contacts. The user simply starts typing and the list gets filtered then. Filtering is not really like searching. If you list contains the word foo somewhere and you type oo foo will be filtered out, but if you type fo it will stay even if the list item is call bar foo.

You simply have to enable it:

ListView lv = getListView();
lv.setTextFilterEnabled(true);

I don't know how this is done if you don't have a hardware keyboard. I'm using the droid and starting to type starts the list to filter and to show only matching results.

Janusz
ok, it isn't exactly what i need but it's a good solution. thanks!
xger86x
+1  A: 

A very good Example is at http://www.androidpeople.com/android-listview-with-searchbox-sort-items/

Amith GC
This solution creates a new data array and a new ListAdapter for every character typed. I can't believe this is the best way to do it..
Thomas Ahle