android searchable doesn't work

孤街浪徒 提交于 2021-02-06 10:13:46

问题


I'm working around with a searchable action bar. And I gotta a problem that the action bar doesn't react with the searchable activity. THAT IS, I enter something in the action bar's searchable textedit then clicked the submit but nothing happened. The debug tracking shows that the code in my searchresultactivity is never executed. So I'm wondering if there's something wrong in searchable configuration. Well this time I think I've explained the problem clearly and I don't expect any negative votes without any words even spits!

I followed the instruction of developer's document beginning with the manifest.xml, the meta-data is added in the searchresult activity:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".GermanDictionaryActivity"
        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=".SearchResultsActivity">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter> 
        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>
</application>

then I initialized the searchview with my menu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.srhbar, menu);

    // Get the SearchView and set the searchable configuration
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    mSrhView = (SearchView) menu.findItem(R.id.search).getActionView();
    mSrhView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    mSrhView.setSubmitButtonEnabled(true);

    return true;
}

And the searchable item in menu:

<item android:id="@+id/search"
    android:title="@string/srh_title"
    android:showAsAction="collapseActionView|ifRoom"
    android:actionViewClass="android.widget.SearchView" />

Any answer related is appreciated!


回答1:


I solved the problem by changing this line:

searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(getApplicationContext(), SearchResultsActivity.class)));


来源:https://stackoverflow.com/questions/9924734/android-searchable-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!