SearchView不显示自定义建议

mysliwiec_tech

我在使用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;
    }
}
mysliwiec_tech

我已经找到了解决我的问题存在

我不知道为什么会引起问题,但这是由于android:labelandroid:hint在我的可搜索配置中引起的。我已经说过了,strings但它们应该是一个string resource(如文档中所述)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

自定义ContentProvider建议不显示

来自分类Dev

自定义菜单onOpen不显示

来自分类Dev

jqtree不显示自定义属性

来自分类Dev

自定义UICollectionViewCell不显示更改

来自分类Dev

jqtree不显示自定义属性

来自分类Dev

自定义UICollectionViewCell不显示更改

来自分类Dev

自定义菜单onOpen不显示

来自分类Dev

自定义ValidationAttribute不显示错误

来自分类Dev

自定义UITableViewCell-不显示

来自分类Dev

自定义SearchView TextChange

来自分类Dev

将SearchView与自定义searchSuggestAuthority结合使用时,系统中的不需要的建议

来自分类Dev

不显示角度自定义过滤器

来自分类Dev

自定义应用程序图标不显示

来自分类Dev

从TButton继承的自定义按钮不显示

来自分类Dev

Xcode ViewController不显示在自定义类中

来自分类Dev

自定义内容类型中的MediaLibraryPickerField不显示

来自分类Dev

SceneKit –自定义几何图形不显示

来自分类Dev

自定义ListView不显示任何内容

来自分类Dev

自定义适配器不显示数据

来自分类Dev

自定义字体在IOS中不显示(.otf类型)

来自分类Dev

自定义UIButton子类不显示标题

来自分类Dev

自定义布局完全不显示-Android

来自分类Dev

自定义UITableViewCell不显示重新排序控件

来自分类Dev

Unity问候者不显示自定义墙纸

来自分类Dev

CSS:“自定义范围”按钮不显示图标

来自分类Dev

Flask:蓝图不显示自定义错误页面

来自分类Dev

自定义QGraphicsPixmapItem不显示像素图

来自分类Dev

Angular OpenLayers不显示自定义指针功能

来自分类Dev

自定义LinearLayout的子代不显示波纹效果