带搜索的android pass捆绑包
您好,我正在使用android 3.0搜索小部件(不是搜索模式),问题是我需要在搜索词中传递一些参数 Android并没有调用任何这些带搜索的android pass捆绑包,android,android-intent,android-searchmanager,Android,Android Intent,Android Searchmanager,您好,我正在使用android 3.0搜索小部件(不是搜索模式),问题是我需要在搜索词中传递一些参数 Android并没有调用任何这些 searchManager.setOnDismissListener( new OnDismissListener() { @Override public void onDismiss() { // TODO Auto-generated method stub
searchManager.setOnDismissListener( new OnDismissListener() {
@Override
public void onDismiss() {
// TODO Auto-generated method stub
Log.v(MyApp.TAG, "on dismiss search");
}
});
searchManager.setOnCancelListener( new OnCancelListener() {
@Override
public void onCancel() {
// TODO Auto-generated method stub
Log.v(MyApp.TAG, "on dismiss cancel");
}
});
@Override
public boolean onSearchRequested() {
Bundle appData = new Bundle();
appData.putLong("listino_id", this.listino_id);
this.startSearch(null, true, appData, false);
return true;
// return super.onSearchRequested();
}
我需要我的活动不是单身,
这是我的AndroidManifest.xml
<activity android:name=".activity.ListinoProdottiActivity" android:windowSoftInputMode="stateHidden">
<!-- Receives the search request. -->
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<!-- No category needed, because the Intent will specify this class component-->
</intent-filter>
<!-- enable the base activity to send searches to itself -->
<meta-data android:name="android.app.default_searchable"
android:value=".activity.ListinoProdottiActivity" />
<!-- Points to searchable meta data. -->
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
这是我的searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
/>
检查您是否有res/xml/searchable.xml中的android:hint
或android:label
的硬编码字符串。它们必须是字符串资源ID。在搜索小部件中使用捆绑包传递数据:
布局/main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<Button android:id="@+id/button" android:text="test"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/search_label"
android:hint="@string/search_hint"
android:searchMode="showSearchLabelAsBadge"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
android:voiceLanguageModel="free_form"
android:voicePromptText="@string/search_invoke"
android:searchSuggestSelection=" ? "
/>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, SearchTest!</string>
<string name="app_name">Searchtest</string>
<string name="search_label">Search Test</string>
<string name="search_hint">1234</string>
<string name="search_invoke">234</string>
<string name="search_query_results">544545</string>
</resources>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".SearchWidgetExampleTest" >
<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=".ResultActivty" />
</activity>
<activity android:name=".ResultActivty"
android:label="@string/search_query_results">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
<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="@layout/searchable" />
</activity>
</application>
package org.imranandroid.TestSearchexmp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SearchWidgetExampleTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.button);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onSearchRequested();
}
});
}
@Override
public boolean onSearchRequested() {
Bundle appDataBundle = new Bundle();
appDataBundle.putLong("listino_id", 4434724724L);
startSearch("imran", false, appDataBundle, false);
return true;
}
}
SearchWidgetExampleTest.java:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<Button android:id="@+id/button" android:text="test"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/search_label"
android:hint="@string/search_hint"
android:searchMode="showSearchLabelAsBadge"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
android:voiceLanguageModel="free_form"
android:voicePromptText="@string/search_invoke"
android:searchSuggestSelection=" ? "
/>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, SearchTest!</string>
<string name="app_name">Searchtest</string>
<string name="search_label">Search Test</string>
<string name="search_hint">1234</string>
<string name="search_invoke">234</string>
<string name="search_query_results">544545</string>
</resources>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".SearchWidgetExampleTest" >
<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=".ResultActivty" />
</activity>
<activity android:name=".ResultActivty"
android:label="@string/search_query_results">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
<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="@layout/searchable" />
</activity>
</application>
package org.imranandroid.TestSearchexmp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SearchWidgetExampleTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.button);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onSearchRequested();
}
});
}
@Override
public boolean onSearchRequested() {
Bundle appDataBundle = new Bundle();
appDataBundle.putLong("listino_id", 4434724724L);
startSearch("imran", false, appDataBundle, false);
return true;
}
}
快乐编码如果您的呼叫是syncronic(您执行一个搜索请求并等待响应,并且在收到响应之前不执行任何搜索请求),那么就很容易了
只需创建一个helper静态类即可
像这样的事情:
public static Bundle myStaticClass;
.
.
.
myStaticClass.putString(key, value);
callSearch();
.
.
.
afterSearchResponse();
String value = myStaticClass.get(key);
按searchview查询时(可搜索的| | searchview小部件) 您可以将参数传递给searchActivity(searchResultActivity) 通过像这样定义自己的OnQueryTextListener
searchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
Intent searchIntent = new Intent(getApplicationContext(), SearchResultsActivity.class);
searchIntent.putExtra(SearchManager.QUERY, query);
searchIntent.setAction(Intent.ACTION_SEARCH);
startActivity(searchIntent);
return true;
}
});
您可以使用以下命令 菜单中
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_al_ayats, menu);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.action_search).getActionView();
Bundle appData = new Bundle();
appData.putString("hello", "world");
searchView.setAppSearchData(appData);
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
在您的搜索结果活动中,输入以下内容
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_search_result);
handleIntent(getIntent());
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String extrastring=intent.getBundleExtra(SearchManager.APP_DATA).getString("hello");
//use the query to search your data somehow
getSupportActionBar().setTitle(extrastring);
}
}
当你不传递额外的参数时,它会起作用吗?我不能传递额外的参数,因为我不能使用任何“钩子”将参数添加到意图中,这3个函数都不会被调用。请粘贴@xml/searchable的内容。谢谢,它与普通搜索一样工作,但它不使用搜索小部件,如在或如在“请看屏幕截图”中所示,您询问了有关传递数据的问题,所以这是您可以做到的方法,如果我误解了您的意思,请编辑您的问题以使其更易于理解。