SearchView不显示自定义建议

时间:2016-04-17 14:48:20

标签: android android-contentprovider searchview

我在使用SearchView时遇到问题。我想通过点击“放大镜”打开一个活动并从工具栏执行搜索。不幸的是,当我打字时没有出现任何建议...

这就是我所拥有的:

的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;
    }
}

1 个答案:

答案 0 :(得分:0)

我找到了问题there的解决方案。

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