/** * Method used for performing the search and displaying the results. This method is called every * time a letter is introduced in the search field. * * @param query Query used for performing the search */ private void displayResults(String query) { Cursor cursor = mDbHelper.searchByInputText((query != null ? query : "@@@@")); if (cursor != null) { String[] from = new String[] {SearchHelper.COLUMN_NAME}; // Specify the view where we want the results to go int[] to = new int[] {R.id.search_result_text_view}; // Create a simple cursor adapter to keep the search data SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.result_search_item, cursor, from, to); myList.setAdapter(cursorAdapter); // Click listener for the searched item that was selected myList.setOnItemClickListener( new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // Get the cursor, positioned to the corresponding row in the result set Cursor cursor = (Cursor) myList.getItemAtPosition(position); // Get the state's capital from this row in the database. String selectedName = cursor.getString(cursor.getColumnIndexOrThrow("name")); Toast.makeText(SearchableActivity.this, selectedName, Toast.LENGTH_SHORT).show(); // Set the default adapter myList.setAdapter(defaultAdapter); // Find the position for the original list by the selected name from search for (int pos = 0; pos < nameList.size(); pos++) { if (nameList.get(pos).equals(selectedName)) { position = pos; break; } } // Create a handler. This is necessary because the adapter has just been set on the // list again and // the list might not be finished setting the adapter by the time we perform // setSelection. Handler handler = new Handler(); final int finalPosition = position; handler.post( new Runnable() { @Override public void run() { myList.setSelection(finalPosition); } }); searchView.setQuery("", true); } }); } }