Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, as a matter of fact, that is the "normal" use case. What you have to remember is that the fragment that uses the search action bar widget, should be the part that registers it in general, since the widget will get destroyed along with that fragment.</p> <p>Here's how to properly tie it up:</p> <p>in your <code>ListFragment</code> let it be known that you have option menu items...</p> <pre><code>@Override public void onActivityCreated(Bundle savedInstanceState) { .. setHasOptionsMenu(true); .. } </code></pre> <p>then inside the same <code>ListFragment</code> create your options menu by overriding the callback, once you have the SearchView widget reference, register a QueryTextListener callback:</p> <pre><code>@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.grid_default, menu); SearchView searchView = (SearchView)menu.findItem(R.id.grid_default_search).getActionView(); searchView.setOnQueryTextListener(queryListener); } </code></pre> <p>create your search widget via either programmatically (Java) or declaratively (XML) here is XML (named grid_default.xml, from above):</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;menu xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:id="@+id/grid_default_search" android:icon="@android:drawable/ic_menu_search" android:title="search" android:showAsAction="always" android:actionViewClass="android.widget.SearchView" /&gt; &lt;!-- other items or whatever --&gt; &lt;/menu&gt; </code></pre> <p>now back in your <code>ListFragment</code> you need to create the <code>queryListener</code> we registered above:</p> <pre><code>private String grid_currentQuery = null; // holds the current query... final private OnQueryTextListener queryListener = new OnQueryTextListener() { @Override public boolean onQueryTextChange(String newText) { if (TextUtils.isEmpty(newText)) { getActivity().getActionBar().setSubtitle("List"); grid_currentQuery = null; } else { getActivity().getActionBar().setSubtitle("List - Searching for: " + newText); grid_currentQuery = newText; } getLoaderManager().restartLoader(0, null, MyListFragment.this); return false; } @Override public boolean onQueryTextSubmit(String query) { Toast.makeText(getActivity(), "Searching for: " + query + "...", Toast.LENGTH_SHORT).show(); return false; } }; </code></pre> <p>now you know when a query is changed or submitted, in my example, I re-query anytime a key is pressed because my database was small and fast with results, you might need to change this.. but you'll notice how I am just using the widget to set a private class field inside the <code>ListFragment</code> to the current text of the search widget (<code>grid_currentQuery</code>) and then I am just calling <code>getLoaderManager().restartLoader(0, null, MyListFragment.this);</code> where inside my <code>onCreateLoader</code> I am just updating the query I used like this:</p> <pre><code>@Override public Loader&lt;Cursor&gt; onCreateLoader(int id, Bundle bargs) { String sort = "SortColumn ASC"; String[] grid_columns = new String[] { "ColumnA", "ColumnB", "Etc..." }; String grid_whereClause = "ColumnToSearchBy LIKE ?" if (!TextUtils.isEmpty(grid_currentQuery)) { return new CursorLoader(getActivity(), DataProvider.CONTENT_URI, grid_columns, grid_whereClause, new String[] { grid_currentQuery + "%" }, sort); } return new CursorLoader(getActivity(), DataProvider.CONTENT_URI, grid_columns, null, null, sort); } </code></pre> <p>and that's all you really need to do, I realize this is kind of chopped up but it's like this because there is more stuff that needs to happen, setting the adapter, starting the loader in the first place using <code>getLoaderManager().initLoader(0, null, this);</code> and all the rest of the loader callbacks that need to be handled... but if you are following the best practices set forward by the android folks, (using <code>LoaderManager</code>s in your <code>ListActivity</code>s and <code>ListFragments</code>s it should be pretty easy for you...</p> <p>I hope this helps feel free to ask for clarification in the comments if you need to -ck</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload