1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.searchabledict; 18 19 import android.app.Activity; 20 import android.app.SearchManager; 21 import android.content.Intent; 22 import android.database.Cursor; 23 import android.net.Uri; 24 import android.os.Bundle; 25 import android.view.Menu; 26 import android.view.MenuInflater; 27 import android.view.MenuItem; 28 import android.view.View; 29 import android.widget.AdapterView; 30 import android.widget.ListView; 31 import android.widget.SimpleCursorAdapter; 32 import android.widget.TextView; 33 import android.widget.AdapterView.OnItemClickListener; 34 35 /** 36 * The main activity for the dictionary. 37 * Displays search results triggered by the search dialog and handles 38 * actions from search suggestions. 39 */ 40 public class SearchableDictionary extends Activity { 41 42 private TextView mTextView; 43 private ListView mListView; 44 45 @Override onCreate(Bundle savedInstanceState)46 public void onCreate(Bundle savedInstanceState) { 47 super.onCreate(savedInstanceState); 48 setContentView(R.layout.main); 49 50 mTextView = (TextView) findViewById(R.id.text); 51 mListView = (ListView) findViewById(R.id.list); 52 53 Intent intent = getIntent(); 54 55 if (Intent.ACTION_VIEW.equals(intent.getAction())) { 56 // handles a click on a search suggestion; launches activity to show word 57 Intent wordIntent = new Intent(this, WordActivity.class); 58 wordIntent.setData(intent.getData()); 59 startActivity(wordIntent); 60 finish(); 61 } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 62 // handles a search query 63 String query = intent.getStringExtra(SearchManager.QUERY); 64 showResults(query); 65 } 66 } 67 68 /** 69 * Searches the dictionary and displays results for the given query. 70 * @param query The search query 71 */ showResults(String query)72 private void showResults(String query) { 73 74 Cursor cursor = managedQuery(DictionaryProvider.CONTENT_URI, null, null, 75 new String[] {query}, null); 76 77 if (cursor == null) { 78 // There are no results 79 mTextView.setText(getString(R.string.no_results, new Object[] {query})); 80 } else { 81 // Display the number of results 82 int count = cursor.getCount(); 83 String countString = getResources().getQuantityString(R.plurals.search_results, 84 count, new Object[] {count, query}); 85 mTextView.setText(countString); 86 87 // Specify the columns we want to display in the result 88 String[] from = new String[] { DictionaryDatabase.KEY_WORD, 89 DictionaryDatabase.KEY_DEFINITION }; 90 91 // Specify the corresponding layout elements where we want the columns to go 92 int[] to = new int[] { R.id.word, 93 R.id.definition }; 94 95 // Create a simple cursor adapter for the definitions and apply them to the ListView 96 SimpleCursorAdapter words = new SimpleCursorAdapter(this, 97 R.layout.result, cursor, from, to); 98 mListView.setAdapter(words); 99 100 // Define the on-click listener for the list items 101 mListView.setOnItemClickListener(new OnItemClickListener() { 102 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 103 // Build the Intent used to open WordActivity with a specific word Uri 104 Intent wordIntent = new Intent(getApplicationContext(), WordActivity.class); 105 Uri data = Uri.withAppendedPath(DictionaryProvider.CONTENT_URI, 106 String.valueOf(id)); 107 wordIntent.setData(data); 108 startActivity(wordIntent); 109 } 110 }); 111 } 112 } 113 114 @Override onCreateOptionsMenu(Menu menu)115 public boolean onCreateOptionsMenu(Menu menu) { 116 MenuInflater inflater = getMenuInflater(); 117 inflater.inflate(R.menu.options_menu, menu); 118 return true; 119 } 120 121 @Override onOptionsItemSelected(MenuItem item)122 public boolean onOptionsItemSelected(MenuItem item) { 123 switch (item.getItemId()) { 124 case R.id.search: 125 onSearchRequested(); 126 return true; 127 default: 128 return false; 129 } 130 } 131 } 132