Embed custom search with SearchView

I want to implement a search in my application, but I do not want to use a separate activity to display search results. Instead, I want to use the list of offers that appears under SearchView .

I can use setOnQueryTextListener on a SearchView , listen for input and search for results. But how can I add these results to the list below SearchView ? Suppose I search in List<String> .

+4
source share
2 answers

What you need to create is a content provider . That way, you can add custom results to your SearchView and add an autocomplete function to it whenever a user enters something.

If I don’t remember correctly, in one of my projects I did something similar, and it did not take too much time.

I find this to be useful: Include an AutoCompleteTextView in the SearchView in the ActionBar instead

And also this: SearchManager - adding custom offers

Hope this helps.

N.

+2
source

I implemented a search in my application using EditText, which accepts a search string.
And below this EditText, I have a ListView on which I want to perform a 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 of EditText submenus changes according to which search text is entered into 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 searchList () function performs the actual search.

  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 an ArrayList that appears in the ListView, and adapter is the ListView adapter.
Hope this helps you in some way.

+1
source

Source: https://habr.com/ru/post/1440632/


All Articles