2011-01-02 5 views
1

Salut im essayant d'utiliser une activité de recherches dans mon application, mais lorsque le bouton de recherche est rien pressé arriveandroid interrogeable ne pas ouvrir

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.test.test" android:versionCode="1" android:versionName="1.0.0" android:configChanges="keyboardHidden|orientation"> 
     <uses-sdk android:minSdkVersion="7"/> 
     <application android:icon="@drawable/icon" android:label="Test"> 
      <activity android:name=".Test" android:label="Test" android:debuggable="true" android:theme="@android:style/Theme.NoTitleBar" android:launchMode="singleTask"> 
       <intent-filter> 
        <action android:name="android.intent.action.MAIN"/> 
        <category android:name="android.intent.category.LAUNCHER"/> 
       </intent-filter> 
      </activity> 
      <activity android:name=".Searchable"> 
       <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> 
      <meta-data android:name="android.app.default_searchable" android:value=".Searchable"/> 
     </application> 
    </manifest> 

Searchable.xml (res/xml/interrogeable .xml)

<?xml version="1.0" encoding="utf-8"?> 
    <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="Search" android:hint="Perform Search"> 
    </searchable> 

Searchable.java (src/com/test/test/Searchable.java)

package com.test.test; 

import android.app.Activity; 
import android.app.SearchManager; 
import android.content.Intent; 
import android.os.Bundle; 

public class Searchable extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     handleIntent(getIntent()); 
    } 
    @Override 
    protected void onNewIntent(Intent intent) { 
     setIntent(intent); 
     handleIntent(intent); 
    } 
    private void handleIntent(Intent intent) { 
     if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
      String query = intent.getStringExtra(SearchManager.QUERY); 
     } 
    } 
} 

TIA,

ng93

+0

N'êtes-vous pas en train d'oublier de montrer une vue? Comme: setContentView (R.layout.main); –

Répondre

3

Votre activité Searchable doit faire quelque chose - et afficher réellement des résultats.

Je viens d'écrire un tel que suggéré par une réponse à une question que je posais Rechercher hier Trying to filter a ListView with runQueryOnBackgroundThread but nothing happens - what am I missing?

à cette documentation pour savoir comment intégrer la prise en charge intégrée de recherche: Using the Android Search Dialog et regardez cet article sur la façon d'offrir des suggestions lorsque le client tape: Adding Custom Suggestions

Votre classe Searchable doit en faire un peu plus après avoir récupéré la chaîne de requête. Par exemple, dans mon activité après avoir obtenu la chaîne de requête que je fais ceci:

 showResults(query); 

et cette méthode ressemble à ceci:

private void showResults(String query) { 
    // Load the list of countries based on the query 
    Cursor countryCursor = myDbHelper.getCountryList (query); 
    startManagingCursor (countryCursor); 

    // Hook up the query results to the list view 
    String[] from = new String[] { 
     WorldInfoDatabaseAdapter.KEY_COUNTRYCODE, WorldInfoDatabaseAdapter.KEY_COUNTRYNAME 
    }; 
    int[] to = new int[] { 
     R.id.countryflag, R.id.countryname 
    }; 
    SimpleCursorAdapter adapter = new SimpleCursorAdapter (this, 
      R.layout.country_list_row, countryCursor, from, to); 
    adapter.setViewBinder (new FlagViewBinder()); 

    myCountryList.setAdapter (adapter); 
    myCountryList.setOnItemClickListener (new OnItemClickListener() { 

     @Override 
     public void onItemClick (AdapterView<?> parent, View view, int position, long id) { 
      String countryName = myDbHelper.getCountryByID (id); 
      if (countryName == null) { 
       new AlertDialog.Builder (SelectCountryActivity.this).setMessage (
         "Internal error: Cannot find the country with id'" + id + "'.").show(); 

       return; 
      } 

      // Package up the country name to return 
      Intent newCountryIntent = new Intent (myCountryList.getContext(), WorldInfoActivity.class); 
      newCountryIntent.putExtra (WorldInfoActivity.KEY_SELECTED_COUNTRY, countryName); 
      startActivity (newCountryIntent); 

      startActivity (newCountryIntent); 
      finish(); 
     } 
    }); 
} 

Le myDbHelper membre interroge ma base de données pour les pays qui ont passé dans la chaîne de requête et les affiche dans une liste. Mon activité a une mise en page qui ressemble à ceci:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    style="?pageBackground"> 
    <TextView 
     android:id="@+id/selectdlgtitle" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/select_country_title" 
     style="?dlgTitle" /> 
    <ListView 
     android:id="@+id/countrylist" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:cacheColorHint="#00000000" 
     android:padding="2px"/> 
    </LinearLayout> 

Vous pouvez probablement faire sans appel setViewBinder - je besoin parce que je traduis le champ de code de pays en une icône de drapeau.

+0

Merci, mais ne peut toujours pas le faire fonctionner. J'ai ajouté System.out.println ("Recherche"); à onCreate() et onNewIntent() mais ne les voyant pas non plus. – ng93

+5

Je lisais juste plus sur ce sujet ce matin et je me souviens de plusieurs personnes qui disaient que ça ne pouvait pas fonctionner avec des chaînes de caractères constantes dans le fichier searchable.xml. Deux personnes ont rapporté qu'avec des ficelles constantes, elles ont passé beaucoup de temps à déboguer et cela n'a jamais fonctionné. Quand ils sont passés aux ressources de cordes tout était bon. Salut, je suis confronté au même problème, et j'utilise en fait des ressources de chaîne dans le fichier searchable.xml, et j'imprime également la chaîne de requête dans une vue de texte dans une méthode –

+1

commutée en ressources de chaîne et fonctionne parfaitement: D acclame – ng93