SearchView 不显示自定义建议

SearchView doesn't show custom suggestions

我在使用 SearchView 时遇到问题。我想打开 activity 并通过点击 "magnifying glass" 从工具栏执行搜索。不幸的是,当我输入时没有显示任何建议...

这是我的:

Manifest.xml

<activity android:name=".SearchableActivity"
    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>

<provider
    android:authorities="com.example.provider"
    android:name="com.example.MyCustomSearchSuggestions">
</provider>

SearchableActivity.java

public class SearchableActivity extends AppCompatActivity {

    private static final String TAG = "SearchableActivity";

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

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

    private void handleIntent(Intent intent) {
        Log.d(TAG, "handleIntent: ");
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            Log.d(TAG, "handleIntent: ACTION_SEARCH");
            String searchQuery = intent.getStringExtra(SearchManager.QUERY);

        } else if (Intent.ACTION_VIEW.equals(intent.getAction())) {
            Log.d(TAG, "handleIntent: ACTION_VIEW");
            Uri detailUri = intent.getData();
            long id = Long.parseLong(detailUri.getLastPathSegment());
            finish();
        } else {
            Log.d(TAG, "handleIntent: " + intent.getAction());
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the options menu from XML
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_profile, menu);

        // Get the SearchView and set the searchable configuration
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.search_profile).getActionView();
        // Assumes current activity is the searchable activity
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default

        return true;
    }

}

searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="Search"
    android:hint="Search user"
    android:searchSuggestAuthority="com.example.provider"
    android:searchSuggestIntentAction="android.intent.action.VIEW" >
</searchable>

menu_profile.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.android.ProfileActivity">
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never" />
    <item android:id="@+id/search_profile"
        android:title="Szukaj"
        android:icon="@drawable/ic_search"
        app:showAsAction="collapseActionView|ifRoom"
        app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

MyCustomSearchSuggestions.java - 建议的内容提供者

public class MyCustomSearchSuggestions extends ContentProvider {

    private static final String TAG = "MyCustomSearchSuggestio";

    @Override
    public boolean onCreate() {
        Log.d(TAG, "onCreate: ");

        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

        Log.d(TAG, "query: ");

        String[] columns = {"_ID", SearchManager.SUGGEST_COLUMN_TEXT_1};
        MatrixCursor matrixCursor = new MatrixCursor(columns);

        matrixCursor.addRow(new Object[] {1, "test"});
        matrixCursor.addRow(new Object[] {2, "test1"});
        return matrixCursor;
    }
}

我找到了解决问题的方法 there

我不知道为什么会出现问题,但这是因为我的可搜索配置中的 android:labelandroid:hint。我放了 strings 但他们认为是 string resource(如 documentation 中所述)。