androidsearchandroid-emulatorfull-text-searchlistactivity

Search in Android


I have a app in which i wish to add search feature, i am trying to implement as told in developer.android but the activity does not start when i click on search in emulator, what is the problem?

  1. SearchActivity.java

    public class SearchActivity extends ListActivity 
    {
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
       handleIntent(getIntent());
    }
    
     @Override
     public void onNewIntent(Intent intent) {
       super.onNewIntent(intent);
       setIntent(intent);
       handleIntent(intent);
     }
    
     public void onListItemClick(ListView l,
       View v, int position, long id) {
        // call detail activity for clicked entry
     }
    
     private void handleIntent(Intent intent) {
      if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
      String query =intent.getStringExtra(SearchManager.QUERY);
       doSearch(query);
     }
    }
      private void doSearch(String queryStr) {
        // get a Cursor, prepare the ListAdapter
        // and set it
     }
    }
    
  2. xml/searchable.xml

    <?xml version="1.0" encoding="utf-8"?>
    <searchable xmlns:android="http://schemas.android.com/apk/res/android"
        android:label="@string/app_name"
        android:hint="@string/srchHint"
        android:inputType="textCapCharacters">
    </searchable>
    
  3. AndroidManifest.xml(code below does not show other activities)

    <application>
         <!-- other actvities -->
          <activity android:label="@string/app_name" android:launchMode="singleTop" 
                 android:name=".SearchActivity">
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
          </intent-filter>
            <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
         </activity>
    </application>
    

Things already tried:

1. looks at similar question and their solution at StackOverflow

2. Look at tutorials at grokkingandroid and edumobile, they did not help my problem.

3. Spend time asking people at chat.stackoverflow

Problem/Questions

1. Ok i have doSearch() method, but how exactly fire a query and how to display results from db(take a small db to show example how exactly, if you can), how to populate results.

2. The search activity does not start when i click on search in the emulator.

3. What exactly to keep in mind, while designing search apart from the developer.android stuff, the other details which many times the site does not tell.

4. How to perform a full-text search, i read about it on developer.android , how to have my db be good to have a FTS

P.S: I am new to learning android, so if there is a rookie mistake, please bear with it, do not skip over steps.


Solution

  • So the Search is crucial factor on how usable your application actually is and provide necessary user enhancement

    1. This is one of the most easiest way to handle search

    private void handleIntent(Intent intent) { 
          if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
             String query = 
                   intent.getStringExtra(SearchManager.QUERY); 
             doSearch(query); 
          } 
       }    
    
       private void doSearch(String queryStr) 
           { 
           // get a Cursor/ArrayList, prepare the ListAdapter
           // and set it
           //helper.getSearchRecords(queryStr);//helper is a SQLiteOpenHelper Object
           //set it to adapter or get return result and proceed as you wish
           //personally i prefer ArrayAdapter
           ListView lstResult=(ListView)findViewById(R.id.lstVwResult);
           lstResult.setAdapter(null);
        SearchAdapter adapter=new SearchAdapter(getApplicationContext(), R.id.txvPersonId, helper.getSearchRecords(queryStr));
            lstResult.setAdapter(adapter);
       } 
    

    2. The reason for SearchActivity not starting is <meta-data> need to be define outside the activity also, like this

    <application>
    <activity android:label="@string/app_name" android:launchMode="singleTop" 
                     android:name=".SearchActivity">
                <intent-filter>
                    <action android:name="android.intent.action.SEARCH" />
                </intent-filter>
                <meta-data android:name="android.app.searchable"  android:resource="@xml/searchable" />
    </activity>
    
    <!--define metadata outside activity in application tag-->
    <meta-data android:name="android.app.default_searchable" android:value=".SearchActivity" />
    

    3. One of the key things to keep in mind, why your are adding search feature to your app, it its a Contacts App, clicking on retrieve results would show more detail to particular contact, but for a restaurant app it will show detail for where restaurant details, you as developer should develop it to crater user needs and enhance user experience.

    4. For having Full-Text Search, it is not always necessary, it solely depends on how you wish to query for results to db, which fields will you consider and how are they relevant to main purpose of your application,

    For more Reference