Android 如何处理已单击项目的搜索建议

Android 如何处理已单击项目的搜索建议,android,android-listview,android-contentprovider,search-suggestion,Android,Android Listview,Android Contentprovider,Search Suggestion,我使用了官方Android示例代码SearchableDictionary链接:,它为我们提供了一个搜索界面,您可以在其中搜索一个单词,您有两个选项: 1-输入关键字并单击搜索图标键盘,这样将显示所有匹配结果的列表视图。在ListView中单击一个单词以检索定义 2-输入关键字,每次searchView关键字更改时,都会自动显示一个建议列表,因此您可以单击建议以检索她的定义 这是在大列表视图(而不是建议列表)中单击某个项目时调用的搜索函数的代码 private void doSearc

我使用了官方Android示例代码SearchableDictionary链接:,它为我们提供了一个搜索界面,您可以在其中搜索一个单词,您有两个选项:

1-输入关键字并单击搜索图标键盘,这样将显示所有匹配结果的列表视图。在ListView中单击一个单词以检索定义

2-输入关键字,每次searchView关键字更改时,都会自动显示一个建议列表,因此您可以单击建议以检索她的定义

这是在大列表视图(而不是建议列表)中单击某个项目时调用的搜索函数的代码

     private void doSearch(String queryStr) { 
            // get a Cursor, prepare the ListAdapter and set it
     final DataBaseHelper myDbHelper = new DataBaseHelper(this);
     myDbHelper.openDataBase();
     Cursor cursor = managedQuery(DictionaryProvider.CONTENT_URI, null, null,
             new String[] {queryStr}, null);
    //Cursor cursor = myDbHelper.fetchListItems(queryStr);
    //cursorr.moveToFirst(); // moves the cursor to the first row in the result set, returns false if the result set is empty.

    startManagingCursor(cursor); /* Managing cursors take care of closing the cursor when
    the activity is destroyed, but they do more than that as well: they will be
    deactivated and required as the activities is stopped and restarted. */

    // set the custom list adapter
    // setListAdapter(new MyListAdapter(this, cursor));

     // Specify the columns we want to display in the result
     String[] from = new String[] { DataBaseHelper.KEY_WORD,
                                    DataBaseHelper.KEY_DEFINITION };

     // Specify the corresponding layout elements where we want the columns to go
     int[] to = new int[] { R.id.title,
                            R.id.details };

     // Create a simple cursor adapter for the definitions and apply them to the ListView
     SimpleCursorAdapter words = new SimpleCursorAdapter(this,
                                   R.layout.list_item_with_description, cursor, from, to);
     final ListView mListView = (ListView) findViewById(R.id.list);
     mListView.setAdapter(words);
    // search_keyword = queryStr ;

     // Define the on-click listener for the list items
     mListView.setOnItemClickListener(new OnItemClickListener() {
         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


             // Build the Intent used to open WordActivity with a specific word Uri
             Intent wordIntent = new Intent(getApplicationContext(), DefinitionActivity.class);
            // final Bundle bundle = new Bundle();


             Uri data = Uri.withAppendedPath(DictionaryProvider.CONTENT_URI,
                                             String.valueOf(id));


             Log.d(TAG,"clicked row id="+id);

             wordIntent.setData(data);
             wordIntent.putExtra("clicked_item_id",id);

             startActivity(wordIntent);
         }
     });

如您所见,我们可以处理匹配单词列表中单击的项目,但如何处理建议小列表中单击的建议?我想捕获单击的建议的id,而不是大listview中单击的项目的id。我如何做到这一点?

单击搜索建议时,将向您的可搜索活动发送意图。您可以通过可搜索的XML通过如下配置android:searchSuggestIntentAction属性来定义此意图的操作字段:

<searchable 
...
    android:searchSuggestIntentAction = "android.intent.action.VIEW">

单击搜索建议时,将向您的可搜索活动发送意图。您可以通过可搜索的XML通过如下配置android:searchSuggestIntentAction属性来定义此意图的操作字段:

<searchable 
...
    android:searchSuggestIntentAction = "android.intent.action.VIEW">