我一直在尝试将我的搜索结果结果重定向到onquerysubmit上的另一个活动,但该应用只显示了一个空白页面。我一直在尝试调试它,但在onquerysubmit返回false之后,调试器只是继续显示应用程序正在运行。
这是我的AndroidManifest.xml
<activity
android:name=".UniversalSearchActivity"
android:label="@string/title_activity_universal_search"
android:theme="@style/AppTheme"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<activity
android:name=".UniversalSearchResultActivity"
android:label="@string/title_activity_report_error"
android:parentActivityName=".UniversalSearchActivity"
android:theme="@style/AppTheme">
</activity>
UniversalSearchActivity.java
public class UniversalSearchActivity extends AppCompatActivity {
private RelativeLayout searchSuggestionsLayout;
private ListView searchListView;
private SearchView searchView;
private UniversalSearchAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_universal_search);
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar_universal_search);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
searchSuggestionsLayout = (RelativeLayout) findViewById(R.id.search_suggestions_container);
searchListView = (ListView) findViewById(R.id.search_suggestions_list);
adapter = new UniversalSearchAdapter(this, this.getSearch());
searchListView.setAdapter(adapter);
searchListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "You clicked an item in the list view!",
Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem searchItem = menu.findItem(R.id.search);
searchItem.expandActionView();
SearchManager searchManager =
(SearchManager) this.getSystemService(Context.SEARCH_SERVICE);
searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setSearchableInfo(
searchManager.getSearchableInfo(new ComponentName(this,
UniversalSearchResultActivity.class)));
searchView.setQueryHint(getString(R.string.placeholder_universal_search));
searchView.setSubmitButtonEnabled(true);
searchView.setIconified(false);
searchView.requestFocus();
menu.add("Search")
.setIcon(R.drawable.ic_search)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
if(query == "") {
Toast.makeText(getApplicationContext(), "Please type a hint", Toast.LENGTH_SHORT)
.show();
} else {
Intent intent = new Intent(getApplicationContext(),
UniversalSearchResultActivity.class);
startActivity(intent);
}
return false;
}
};
searchView.setOnQueryTextListener(queryTextListener);
return super.onCreateOptionsMenu(menu);
}
public ArrayList<UniversalSearch> getSearch() {
ArrayList<UniversalSearch> searches = new ArrayList<UniversalSearch>();
UniversalSearch universalSearch;
for (int i=0; i<names.length; i++) {
universalSearch = new UniversalSearch(names[i], address[i], images[i]);
searches.add(universalSearch);
}
return searches;
}
}
UniversalSearchResultActivity.java
public class UniversalSearchResultActivity extends AppCompatActivity {
private RelativeLayout searchResultLayout;
private ListView searchResultListView;
private UniversalSearchAdapter universalSearchAdapter;
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.activity_universal_search_result);
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar_universal_search_result);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Toast.makeText(getApplicationContext(), "I'm heeeere!!!", Toast.LENGTH_SHORT).show();
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);
Intent resultIntent = new Intent(getApplicationContext(),
UniversalSearchResultActivity.class);
resultIntent.putExtra(SearchManager.QUERY, query);
}
}
}
当使用onquerytextchange时,searchview工作正常,但在onquerytextsubmit的情况下,它没有。请告诉我我做错了什么。谢谢!