简体   繁体   中英

How to implement search interface in android using searchView widget

I have completely followed the Docs on this regard.

  1. I made searchable.xml file
  2. added new activity to handle the query once search is made.
  3. I updated the manifest file accordingly.

My problem is that the new activity is not being called at all as if there is something wrong with the manifest.

Here's the xml file:

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="@string/search_hint" >
</searchable>

Here's the manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sultanraja.notes">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <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>
        <activity android:name=".NoteActivity" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

And here's the new activity code meanwhile:

public class SearchResultsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_results);
        handleIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {

        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            Toast.makeText(getApplicationContext(),"your Query is " + query, Toast.LENGTH_SHORT).show();
            //use the query to search your data somehow
        }
    }
}

The Toast is never happening and a breakpoint in the onCreate method is never reached.

what is the problem?

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {  
        @Override  
        public boolean onQueryTextSubmit(String query) {  

            if(list.contains(query)){  
                adapter.getFilter().filter(query);  
            }else{  
                Toast.makeText(MainActivity.this, "No Match found",Toast.LENGTH_LONG).show();  
            }  
            return false;  
        }  

        @Override  
        public boolean onQueryTextChange(String newText) {  
        //    adapter.getFilter().filter(newText);  
            return false;  
        }  
    });  

I found the problem in the manifest file:

I added the following:

 <meta-data
                android:name="android.app.default_searchable"
                android:value=".SearchResultsActivity" />

this part was added to mainActivity and that was not part of the documentation. thanks.

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