Ich habe diese kleine Testanwendung geschrieben, um das Problem zu demonstrieren, dass die durchsuchbare Aktivität nicht gestartet wird, wenn der Benutzer die Suchtaste drückt die Tastatur.Android assisted Search: Die Suchschaltfläche ruft die durchsuchbare Aktivität nicht auf (Andere Lösungen haben nicht geholfen)
Ich habe die developer guides verfolgt, aber von meiner Web-Suche, stellt sich heraus, dass der offizielle Entwickler Leitfaden einige Punkte verfehlt.Aus meinem SO suchen (die nicht helfen):
Reference 1: Gelöst nach Tag in dem im Manifest Element hinzufügen. Ich schaute auch in die Manifest des Beispiels "User Dictionary" (Ich weiß nicht, wo kann ich finden Sie die Beispiele online, oder ich würde es verlinken). Dieses Tag ist dort in das Anwendungselement.
Reference 2: Die "android: label" und "android: Hinweis" in res/xml/searchable.xml Verweise auf String-Ressourcen und nicht hart codiert Strings sein müssen. Meine sind.
Reference 3: Add einen tag mit "android: name =" android.app.default_searchable " "(und" android: value =". < durchsuchbar-Aktivität-name>“„) im Manifest in der Aktivität von wo die Suche initiiert wird. Versucht dies schien nicht zu funktionieren.
Reference 4: "Ihre durchsuchbare Aktivität muss etwas tun - und tatsächlich Ergebnisse anzeigen." Meine tut es, empfängt die Absicht mit der Aktion ACTION_SEARCH und übergibt die Suchabfragezeichenfolge von der Absicht an eine Methode mit dem Namen "performSearch (string)", die die Zeichenfolge in einer Textansicht anzeigt.
Also, was mache ich falsch, und was kann ich tun, diese zu lösen?
Code:MainActivity.java - Hat eine einzelne Suche - gibt der Benutzer die Abfrage und drückt die Suchtaste auf der Tastatur.
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/durchsuchbar. xml - Die durchsuchbare Konfiguration
<?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>
Manifestdatei
<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: es ist verrückt, dass ich eine ähnliche App mit der Suche dilogue anstelle der Such-Widget, das perfekt funktioniert schrieb.
Ich habe versucht, es in Eclipse zu debuggen, aber das Debuggen beendet, weil die TestTwoActivity
(die durchsuchbare Aktivität) einfach nicht gestartet wird.
ich hatte vergessen, und ich sah meinen Code hunderte Male aber dies nicht sehen, meine schlecht. Vielen Dank. – Solace