简体   繁体   中英

Android search from searchable activity

I have an Activity that holds a list. Via the Android onSearchRequested() I implemented a search. The results are shown as a list with the same adapter in another Activity. Working fine so far.

Also, I want to be able to search from that second Activity showing the new results in the same list.

My AndroidManifest.xml for the two activities:

<activity android:name=".ListActivity" android:label="List">
    <meta-data android:name="android.app.default_searchable" android:value=".SearchActivity" />
</activity>

<activity android:name=".SearchActivity" android:label="Results">
    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
    </intent-filter>
    <meta-data android:name="android.app.searchable" android:resource="@xml/search" android:value=".SearchActivity" />
</activity>

The onResume(): onResume():

@Override
protected void onResume() {
    super.onResume();
    Intent queryIntent = getIntent();
    String value = queryIntent.getStringExtra(SearchManager.QUERY);
    setView(value);
}

The setView() method does a foreach loop through all objects adding them to a result-array which is used for a new Adapter that the list shows.

ca = new CustomAdapter(this, R.layout.customadapter, resultArray);
list.setAdapter(pa);
list.invalidate();

When trying to search from the second Activity the search bar appears, I can enter my search value, send it - but the list doesn't change (and even the keyboard stays). What's missing?

Edit: Tried to make it easier to understand.

Found that question which describes kind of the same problem.

Instead of overriding onResume() I have to override onNewIntent()

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);      
    String value = intent.getStringExtra(SearchManager.QUERY);
    setView(value);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM