2014-12-19 34 views
6

Ho scritto questa piccola applicazione di prova per dimostrare il problema, ovvero l'attività di ricerca non viene avviata quando l'utente preme il pulsante di ricerca su la tastiera.Ricerca assistita da Android: il pulsante di ricerca non richiama l'attività ricercabile (Altre soluzioni non hanno aiutato)

Ho seguito lo developer guides, ma dalla mia ricerca web, , risulta che la guida per gli sviluppatori ufficiale manca di alcuni punti.Dal mio SO ricerca (che non ha aiutato):

  • Reference 1: risolto con l'aggiunta di tag nell'elemento nel manifesto. Ho anche esaminato il manifest dell'esempio "User Dictionary" (non so dove posso trovare gli esempi online, o vorrei collegarmi ad esso) . Questo tag è presente nell'elemento .

  • Reference 2: Il "android: label" e "android: hint" in res/xml/searchable.xml deve essere riferimenti a risorse stringa e non stringhe hard coded. I miei sono.

  • Reference 3: Aggiungi un tag con "android: name =" " "(e" android.app.default_searchable android: value =". <-activity-name ricercabile>" ") nel manifesto in la Attività da dove verrà avviata la ricerca. Provato questo, non sembra funzionare.

  • "L'attività ricercabile deve fare qualcosa - e visualizzare effettivamente i risultati." Il mio, riceve l'intento con l'azione ACTION_SEARCH e passa la stringa di query di ricerca recuperata dall'intenzione a un metodo denominato "performSearch (stringa)" che visualizza la stringa in una visualizzazione di testo.

Così che cosa sto facendo male, e che cosa posso fare per risolvere questo problema?

Codice:MainActivity.java - ha un unico SearchView - l'utente digita la query e preme il pulsante di ricerca sulla tastiera.

public class MainActivity extends ActionBarActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 
} 

TestTwoActivity.java

public class TestTwoActivity extends Activity { 
     TextView tv; 
     private static final String TAG = TestTwoActivity.class.getSimpleName(); 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_test_two); 

      /** 
      * The following code enables assisted search on the SearchView by calling setSearchableInfo() and passing it our SearchableInfo object. 
      */ 
      SearchView searchView = (SearchView) findViewById(R.id.searchActivity_searchView); 
      // SearchManager => provides access to the system search services. 

      // Context.getSystemService() => Return the handle to a system-level 
      // service by name. The class of the returned object varies by the 
      // requested name. 

      // Context.SEARCH_SERVICE => Returns a SearchManager for handling search 

      // Context = Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android 
      // system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching 
      // activities, broadcasting and receiving intents, etc. 

      // Activity.getComponentName = Returns a complete component name for this Activity 

      SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
      searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 

      /** 
      * If the search is executed from another activity, the query is sent to this (searchable) activity in an Intent with ACTION_SEARCH action. 
      */ 
      // getIntent() Returns the intent that started this Activity 
      Intent intent = getIntent(); 
      if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
       Log.i(TAG, "Search Query Delivered");//check 
       String searchQuery = intent.getStringExtra(SearchManager.QUERY); 
       performSearch(searchQuery); 
      } 

     } 

     private void performSearch(String searchQuery) { 
      //Just for testing purposes, I am simply printing the search query delivered to this searchable activity in a textview. 
      tv = (TextView) findViewById(R.id.testTwoActivity_textView); 
      tv.setText(searchQuery); 
     } 
} 

res/xml/ricercabile.xml - La configurazione ricercabile

<?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/searchViewHint" > 
</searchable> 

manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.tests" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="21" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 

     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <activity 
      android:name=".TestTwoActivity" 
      android:label="@string/title_activity_test_two" > 
      <intent-filter> 
       <action android:name="android.intent.action.SEARCH"/> <!-- Declares the activity to accept ACTION_SEARCH intent --> 
      </intent-filter> 
       <meta-data 
        android:name="android.app.searchable" 
        android:resource="@xml/searchable" /> <!-- Specifies the searchable configuration to use --> 
     </activity> 

     <!-- Points to searchable activity so the whole app can invoke search. --> 
     <meta-data android:name="android.app.default_searchable" 
        android:value=".TestTwoActivity" /> 

    </application> 

</manifest> 

Layouts:

activity_main.xml

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

    android:orientation="vertical" 

    android:layout_width="match_parent" 
    android:layout_height="match_parent" 

    tools:context="com.tests.MainActivity" > 

    <android.support.v7.widget.SearchView 
     android:id="@+id/searchActivity_searchView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     /> 

</LinearLayout> 

activity_test_two.xml

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

    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 

    tools:context="${relativePackage}.${activityClass}" > 

    <android.support.v7.widget.SearchView 
     android:id="@+id/searchActivity_searchView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     /> 

    <TextView 
     android:id="@+id/testTwoActivity_textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

EDIT 1: è pazzesco che ho scritto un'applicazione simile con la dilogue di ricerca al posto del widget di ricerca, che funziona perfettamente.

Ho provato a eseguire il debug in Eclipse ma il debug si interrompe perché il TestTwoActivity (l'attività di ricerca) semplicemente non verrà avviato.

risposta

5

non sono sicuro se hai dimenticato di aggiungere, ma i tuoi MainActivity manca di impostazione informazioni consultabile sul SearchView:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    SearchView searchView = (SearchView) findViewById(R.id.searchActivity_searchView); 
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 
} 

Come nota a margine:

Ho avuto problemi con il meta-tag default_searchable, quando si usano aromi. Sembrava di lavorare solo quando si utilizza il percorso completo (saltando il sapore) per l'attività di ricerca ad es .:

<meta-data 
    android:name="android.app.default_searchable" 
    android:value="com.example.SearchActivity"/> 
+0

Avevo dimenticato che e ho guardato il mio codice centinaia di volte, ma non ho visto questo, il mio male. Grazie. – Solace