gpt4 book ai didi

android - 如何处理搜索建议点击项目

转载 作者:行者123 更新时间:2023-11-29 14:40:20 25 4
gpt4 key购买 nike

我使用了官方 Android 示例代码“SearchableDictionary”(链接:http://jayxie.com/mirrors/android-sdk/resources/samples/SearchableDictionary/index.html),它为我们提供了一个搜索界面,您可以在其中搜索一个词,并且有 2 个选项:

1- 输入您的关键字并单击搜索图标(键盘),所有匹配结果的 ListView 将出现。您单击 ListView 中的单词以检索定义。

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

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

     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 中点击的项目。我该怎么做?

最佳答案

单击搜索建议时,会向您的可搜索 Activity 发送一个 Intent。您可以通过配置 android:searchSuggestIntentAction 属性,通过可搜索的 XML 简单地定义此 Intent 的 Action 字段,如下所示:

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

然后,在您的可搜索 Activity 中:

Intent intent = getIntent();
if (Intent.ACTION_VIEW.equals(intent.getAction()) {
//a suggestion was clicked... do something about it...
}

关于android - 如何处理搜索建议点击项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11901836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com