1

J'ai utilisé l'exemple de code Android officical « SearchableDictionary » (lien: http://jayxie.com/mirrors/android-sdk/resources/samples/SearchableDictionary/index.html), qui nous donne une interface de recherche, où vous pouvez rechercher un mot et vous avez 2 options:Comment gérer une suggestion de recherche élément cliqué

1- Mettez votre mot-clé et cliquez sur l'icône de recherche (clavier) pour afficher une liste de tous les résultats correspondants. Vous cliquez sur un mot dans le ListView pour récupérer la définition.

2- Mettez votre mot-clé et une petite liste de suggestion apparaîtra automatiquement chaque fois que le mot clé searchView change, vous pouvez donc cliquer sur une suggestion pour récupérer sa définition.

Ceci est le code de la fonction de recherche appelé lorsque vous cliquez sur un élément dans la grande liste, pas dans la liste des suggestions.

 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); 
     } 
    }); 

Comme vous pouvez le voir, nous pouvons gérer les éléments cliqués dans la liste des mots qui correspondent, mais comment puis-je gérer les suggestions cliqués dans la petite liste des suggestions? Je veux attraper l'id de la suggestion cliquée, pas l'article cliqué dans la grande liste. Comment puis-je faire cela?

Répondre

7

Lorsqu'une suggestion de recherche est cliquée, une intention est envoyée à votre activité de recherche. Vous pouvez simplement définir le champ d'action de cette intention si votre XML interrogeable en configurant l'androïde: searchSuggestIntentAction attribut comme ceci:

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

Et puis, dans votre activité interrogeable:

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