1

公式の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を取得したいと思います。どうすればこれを作ることができますか?

4

1 に答える 1

7

検索候補をクリックすると、検索可能なアクティビティにインテントが送信されます。次のようにandroid:searchSuggestIntentAction属性を構成することにより、検索可能なXMLを介してこのインテントのアクションフィールドを簡単に定義できます。

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

そして、検索可能なアクティビティで:

Intent intent = getIntent();
if (Intent.ACTION_VIEW.equals(intent.getAction()) {
    //a suggestion was clicked... do something about it...
}
于 2012-10-24T04:18:14.373 に答える