Выбрать главу

 }

 @Override

 public boolean onOptionsItemSelected(MenuItem item) {

  switch (item.getItemId()) {

   case LOCAL_SEARCH_ID:

    onSearchRequested();

    return(true);

   case GLOBAL_SEARCH_ID:

    startSearch(nullfalsenulltrue);

    return(true);

   case CLOSE_ID:

    finish();

    return(true);

  }

  return(super.onOptionsItemSelected(item));

 }

}

This activity takes care of everything related to showing a list of words, even loading the words out of the XML resource. What it does not do is come up with the ListAdapter to put into the ListView — that is delegated to the subclasses.

The main activity — LoremDemo — just uses a ListAdapter for the whole word list:

package com.commonsware.android.search;

import android.content.Intent;

import android.widget.ArrayAdapter;

import android.widget.ListAdapter;

public class LoremDemo extends LoremBase {

 @Override ListAdapter makeMeAnAdapter(Intent intent) {

  return(new ArrayAdapter<String>(this,

   android.R.layout.simple_list_item_1, items));

 }

}

The search activity, though, does things a bit differently. First, it inspects the Intent supplied to the abstract makeMeAnAdapter() method. That Intent comes from either onCreate() or onNewIntent(). If the intent is an ACTION_SEARCH, then we know this is a search. We can get the search query and, in the case of this silly demo, spin through the loaded list of words and find only those containing the search string. That list then gets wrapped in a ListAdapter and returned for display:

package com.commonsware.android.search;

import android.app.SearchManager;

import android.content.Intent;

import android.widget.ArrayAdapter;

import android.widget.ListAdapter;

import java.util.ArrayList;

import java.util.List;

public class LoremSearch extends LoremBase {

 @Override

 ListAdapter makeMeAnAdapter(Intent intent) {

  ListAdapter adapter = null;

  if (intent.getAction().equals(Intent.ACTION_SEARCH)) {

   String query = intent.getStringExtra(SearchManager.QUERY);

   List<String> results = searchItems(query);

   adapter = new ArrayAdapter<String>(this,

    android.R.layout.simple_list_item_1, results);

   setTitle("LoremSearch for: " + query);

  }

  return(adapter);

 }

 private List<String> searchItems(String query) {

  List<String> results = new ArrayList<String>();

  for (String item : items) {

   if (item.indexOf(query) - 1) {

    results.add(item);

   }

  }

  return(results);

 }

}

Update the Manifest

While this implements search, it doesn’t tie it into the Android search system. That requires a few changes to the auto-generated AndroidManifest.xml file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

 package="com.commonsware.android.search">

 <application>

  <activity android:name=".LoremDemo" android:label="LoremDemo">

   <intent-filter>

    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />

   </intent-filter>

   <meta-data android:name="android.app.default_searchable"

    android:value=".LoremSearch" />

  </activity>

  <activity

   android:name=".LoremSearch"

   android:label="LoremSearch"

   android:launchMode="singleTop">

   <intent-filter>

    <action android:name="android.intent.action.SEARCH" />

    <category android:name="android.intent.category.DEFAULT" />

   </intent-filter>

   <meta-data android:name="android.app.searchable"

    android:resource="@xml/searchable" />

  </activity>

 </application>

</manifest>

The changes needed are as follows:

1. The LoremDemo main activity gets a meta-data element, with an android:name of android.app.default_searchable and a android:value of the search implementation class (.LoremSearch).

2. The LoremSearch activity gets an intent filter for android.intent.action.SEARCH, so search intents will be picked up.

3. The LoremSearch activity is set to have android:launchMode="singleTop", which means at most one instance of this activity will be open at any time so we don’t wind up with a whole bunch of little search activities cluttering up the activity stack.

4. The LoremSearch activity gets a meta-data element, with an android:name of android.app.searchable and a android:value of an XML resource containing more information about the search facility offered by this activity (@xml/searchable).