我正在尝试在操作栏中的应用程序中实现搜索小部件,但每次输入搜索查询并按Enter键时,都不会发生任何事情。我认为它与我的AndroidManifest文件有关,但我已经尝试了很多东西而无法解决这个问题。此查询从api获取其结果。我一直试图让这个工作大约6个小时。这是我现在的代码(我尝试了很多不同的东西):
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/search_hint" >
</searchable>
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data android:name="android.app.default_searchable"
android:value=".SearchAppsActivity" />
<activity
android:name="com.android.devon.BrowseAppsActivity"
android:label="@string/title_activity_browse_apps" >
</activity>
<activity android:name=".SearchAppsActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<intent-filter >
<action android:name="android.intent.action.VIEW" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
</application>
SearchAppsActivity
public class SearchAppsActivity extends ListActivity
{
private AppFrenzy restClient;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_browse_apps);
handleIntent(getIntent());
}
public void onNewIntent(Intent intent)
{
setIntent(intent);
handleIntent(intent);
}
public void onListItemClick(ListView l, View v, int position, long id)
{
}
private void handleIntent(Intent intent)
{
if(Intent.ACTION_SEARCH.equals(intent.getAction()))
{
String query = intent.getStringExtra(SearchManager.QUERY);
doSearch(query);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.browse_apps, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
return super.onCreateOptionsMenu( menu );
}
private void doSearch(String queryStr)
{
restClient = AppService.getService();
restClient.searchApps(queryStr, new Callback<List<App>>() {
@Override
public void success(List<App> apps, Response response) {
setListAdapter(new AppListAdapter(SearchAppsActivity.this, apps));
Log.v("test", "search successful");
Log.v("test", "search results:" + apps.toString() );
}
@Override
public void failure(RetrofitError retrofitError) {
Log.v("test", "failure");
}
});
}
}
有关为什么我的搜索未提交的任何想法?
答案 0 :(得分:0)
您可以使用SearchView.OnQueryTextListener解决您的问题。
例如,
在main.xml中
<item
android:id="@+id/action_websearch"
android:actionViewClass="android.widget.SearchView"
android:icon="@android:drawable/ic_menu_search"
android:showAsAction="always"
android:title="@string/search_title"/>
onCreateOptionsMenu() 中的
注意:强> @Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.action_websearch)
.getActionView();
// searchView.setQueryHint("Search Patient name");
searchView.setQueryHint(Html.fromHtml("<font color = #ffffff>"
+ getResources().getString(R.string.hint) + "</font>"));
if (null != searchView) {
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
}
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
public boolean onQueryTextChange(String newText) {
// this is your adapter that will be filtered
} //this method will call while press (click) search button.
public boolean onQueryTextSubmit(String query) {
// add searching stuff here
}
}
return super.onCreateOptionsMenu(menu);
}
Don't need searchable.xml and any permission on androidmanifest.xml.