5

I want to implement search in my app, but I don't want to use a separate Activity to display my search results. Instead, I only want to use the suggestionslist that displays beneath the SearchView.

I can use setOnQueryTextListener on the SearchView, listen for input and search for results. But how can I add these results to the list below the SearchView? Let's just assume I'm searching in a List<String>.

nhaarman
  • 98,571
  • 55
  • 246
  • 278

2 Answers2

3

What you need to create is a Content Provider. In that way you can add custom results to your SearchView and add the autocomplete feature to it whenever the User inputs something.

If I don't remember incorrectly, in one of my projects I have done something similar, and it didn't take too long.

I believe this could be helpful: Turn AutoCompleteTextView into a SearchView in ActionBar instead

And also this: SearchManager - adding custom suggestions

Hope this helps.

N.

Community
  • 1
  • 1
N3sh
  • 881
  • 8
  • 37
1

I have implemented search in my application by using an EditText which takes search string.
And below this EditText I have my ListView on which I want to perform search.

<EditText
    android:id="@+id/searchInput"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/input_patch"
    android:gravity="center_vertical"
    android:hint="@string/search_text"
    android:lines="1"
    android:textColor="@android:color/white"
    android:textSize="16sp" >
</EditText>
<ListView
    android:id="@+id/appsList"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/searchInput"
    android:cacheColorHint="#00000000" >
</ListView>  

The list below the search EditText changes according to what search text is entered in the EditText.

etSearch = (EditText) findViewById(R.id.searchInput);
etSearch.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        searchList();
    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }
    @Override
    public void afterTextChanged(Editable s) {
    }
});  

The function searchList() does the actual searching

  private void searchList() {
    String s = etSearch.getText().toString();
    int textlength = s.length();
    String sApp;
    ArrayList<String> appsListSort = new ArrayList<String>();
    int appSize = list.size();
    for (int i = 0; i < appSize; i++) {
        sApp = list.get(i);
        if (textlength <= sApp.length()) {
            if (s.equalsIgnoreCase((String) sApp.subSequence(0, textlength))) {
                appsListSort.add(list.get(i));
            }
        }
    }
    list.clear();
    for (int j = 0; j < appsListSort.size(); j++) {
        list.add(appsListSort.get(j));
    }
    adapter.notifyDataSetChanged();
}  

here list is the ArrayList which shows in the ListView and adapter is the ListView adapter.
I hope this helps you in some way.

Zeba
  • 3,041
  • 1
  • 28
  • 39