首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何处理搜索建议点击的项目

如何处理搜索建议点击的项目
EN

Stack Overflow用户
提问于 2012-08-10 20:37:34
回答 1查看 3.4K关注 0票数 1

我使用了官方的安卓示例代码"SearchableDictionary“(链接:http://jayxie.com/mirrors/android-sdk/resources/samples/SearchableDictionary/index.html),它为我们提供了一个搜索界面,在那里你可以搜索一个单词,你有两个选择:

1-输入你的关键字并点击搜索图标(键盘),这样所有匹配结果的列表视图就会出现。您可以单击ListView中的一个单词来检索定义。

2-输入你的关键字,每次searchView关键字改变时,会自动出现一个建议列表,这样你就可以点击一个建议来检索她的定义。

这是当您点击大列表视图中的项目时调用的搜索函数的代码,而不是建议列表中的项目。

代码语言:javascript
运行
复制
     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,而不是大列表视图中被点击的项目。我该怎么做呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-24 12:18:14

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

代码语言:javascript
运行
复制
<searchable 
...
    android:searchSuggestIntentAction = "android.intent.action.VIEW">

然后,在您的可搜索活动中:

代码语言:javascript
运行
复制
Intent intent = getIntent();
if (Intent.ACTION_VIEW.equals(intent.getAction()) {
    //a suggestion was clicked... do something about it...
}
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11901836

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档