我已经在我的应用程序中实现了 Search 选项,它在每个活动中都可以正常工作,只有在 SearchActivity (显示搜索结果的活动)中不起作用的东西有人可以告诉我为什么
这是我的 SearchActivity 和清单的代码:
public class SearchActivity extends Activity {
private ListView lv;
ArrayList<Proizvod> proizvodi;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.search_activity);
lv = (ListView)findViewById(R.id.lvSearchResoult);
handleIntent(getIntent());
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Proizvod pom = (Proizvod)arg0.getAdapter().getItem(arg2);
Intent intent = new Intent(SearchActivity.this, SecondActivity.class);
intent.putExtra("izabrani", pom);
intent.putExtra("proizvodi", proizvodi);
startActivity(intent);
}
});
}
@Override
protected void onNewIntent(Intent intent) {
handleIntent(getIntent());
}
private void handleIntent(Intent intent){
if(Intent.ACTION_VIEW.equals(intent.getAction())){
}else if(Intent.ACTION_SEARCH.equals(intent.getAction())){
String query = intent.getStringExtra(SearchManager.QUERY);
showResoults(query);
}
}
private void showResoults(String query){
proizvodi = new ArrayList<Proizvod>();
ProizvodTableAdapter dbAdapter = new ProizvodTableAdapter(this);
dbAdapter.open();
proizvodi = dbAdapter.getProizvdi(query);
dbAdapter.close();
lv.setAdapter(new SearchListAdapter(proizvodi,this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
onSearchRequested();
return true;
default:
return false;
}
}
}
显现:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.katalog"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.katalog.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=".SearchActivity"
android:label="@string/app_name"
android:launchMode="singleTop">
<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>
<activity
android:name=".SecondActivity"
android:label="@string/app_name">
</activity>
<activity android:name=".ShowPicture"></activity>
<activity android:name=".PicturesActivity"></activity>
<meta-data android:name="android.app.default_searchable"
android:value=".SearchActivity" />
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
</manifest>