我用officical的Android示例代碼「SearchableDictionary」(鏈接:http://jayxie.com/mirrors/android-sdk/resources/samples/SearchableDictionary/index.html),這給了我們一個搜索界面,在這裏你可以搜索一個詞,你有2種選擇:如何處理點擊搜索建議項目
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,而不是大列表視圖中的點擊項目。我怎樣才能做到這一點?