Turn AutoCompleteTextView into a SearchView in ActionBar instead
我有一个 AutoCompleteTextView,它为用户提供来自 Google Places API 的自动完成搜索结果。完成后,我发现了 SearchView 以及如何将其放置在 ActionBar 中。我查看了 google 提供的 SearchView 示例,并将其作为起点添加到我的应用程序中(它列出了已安装的应用程序),但我不知道如何从这里开始。我想拥有与 AutoCompleteTextView 相同的功能,但改用 SearchView。
有什么建议吗?
下面提供了整个课程。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
import java.io.BufferedReader;
import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import android.app.Activity; /** private static final String TAG ="PlacesListActivity"; private ResultReceiver mReceiver; private OnSharedPreferenceChangeListener sharedPreferencesListener; /** Called when the activity is first created. */ private SearchView mSearchView; @Override final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.item_list); public void onTextChanged(CharSequence s, int start, int before, int count) { public void beforeTextChanged(CharSequence s, int start, int count, } public void afterTextChanged(Editable s) { } }); @Override MenuInflater inflater = getMenuInflater(); return true; private void setupSearchView(MenuItem searchItem) { if (isAlwaysExpanded()) { SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); // Try to use the"applications" global search provider mSearchView.setOnQueryTextListener(this); public boolean onQueryTextChange(String newText) { public boolean onQueryTextSubmit(String query) { public boolean onClose() { protected boolean isAlwaysExpanded() { class GetPlaces extends AsyncTask<String, Void, ArrayList<String>> { Log.d("PlacesListActivity", googlePlaces.toString()); URLConnection tc = googlePlaces.openConnection(); String line; for (int i = 0; i < ja.length(); i++) { return predictionsArr; @Override for (String string : result) { Log.d("PlacesListActivity","onPostExecute : autoCompleteAdapter" + adapter.getCount()); } } |
更新saxman建议的代码后,我可以看到provider中的查询方法从来没有被调用过:
我的清单文件如下所示:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.rathinavelu.rea" android:versionCode="1" android:versionName="1.0"> <uses-sdk <uses-permission android:name="android.permission.INTERNET" /> <application <activity <uses-library android:name="com.google.android.maps" /> <service <provider </application> </manifest> |
我在清单、内容提供者和清单文件中使用相同的权限。我在菜单中看到了 searchView 并且我没有修改查询方法,所以它应该只返回一行光标。但查询方法永远不会被调用。请帮忙。
我刚刚发现的另一个问题是 searchView 没有显示指定的 search_hint!
提供更多代码
*PlacesListSearchActivity.java*
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
public class PlacesListSearchActivity extends Activity {
private static final String TAG ="PlacesListSearchActivity"; private ResultReceiver mReceiver; private OnSharedPreferenceChangeListener sharedPreferencesListener; /** Called when the activity is first created. */ private SearchView mSearchView; @Override final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.item_list); public void onTextChanged(CharSequence s, int start, int before, int count) { public void beforeTextChanged(CharSequence s, int start, int count, } public void afterTextChanged(Editable s) { } }); @Override SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); // setupSearchView(searchItem); return true; |
places_search.xml
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <AutoCompleteTextView android:id="@+id/autoCompleteTextView1" <TextView |
places_list_search_options_menu.xml
1
2 3 4 5 6 7 8 |
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_search" android:title="@string/menu_search" android:icon="@android:drawable/ic_menu_search" android:showAsAction="collapseActionView|ifRoom" android:actionViewClass="android.widget.SearchView" /> </menu> |
可搜索的.xml
1
2 3 4 5 6 |
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="@string/app_name" android:hint="@string/search_hint" android:searchSuggestAuthority="com.rathinavelu.rea.places.search_suggestion_provider"> </searchable> |
PlacesSuggestionProvider.java
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
public class PlacesSuggestionProvider extends ContentProvider { private static final String LOG_TAG ="PlacesSuggestionProvider"; public static final String AUTHORITY ="com.rathinavelu.rea.places.search_suggestion_provider"; // UriMatcher constant for search suggestions private static final UriMatcher uriMatcher; private static final String[] SEARCH_SUGGEST_COLUMNS = { static { @Override @Override @Override @Override @Override // Use the UriMatcher to see what kind of query we have @Override |
要在 SearchView 中获取 Places Autocomplete API 结果,您首先需要 API 的 ContentProvider。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
import android.app.SearchManager; import android.content.ContentProvider; import android.content.ContentValues; import android.content.UriMatcher; import android.database.Cursor; import android.database.MatrixCursor; import android.net.Uri; import android.provider.BaseColumns; import android.util.Log; public class PlacesSuggestionProvider extends ContentProvider { public static final String AUTHORITY ="com.example.google.places.search_suggestion_provider"; // UriMatcher constant for search suggestions private static final UriMatcher uriMatcher; private static final String[] SEARCH_SUGGEST_COLUMNS = { static { @Override @Override @Override @Override @Override // Use the UriMatcher to see what kind of query we have @Override |
然后将您的 Places Autocomplete API 客户端代码添加到内容提供程序的查询方法中。您提取用户输入如下:
1
|
String query = uri.getLastPathSegment().toLowerCase();
|
将 PlacesSuggestionProvider 添加到您的 AndroidManifest,并确保您的活动具有可搜索的配置。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<application
android:icon="@drawable/ic_launcher" android:label="@string/app_name"> <category android:name="android.intent.category.LAUNCHER" /> <meta-data <provider </manifest> |
并确保您的可搜索配置 (res/xml/searchable.xml) 具有搜索建议权限。
1
2 3 4 5 6 |
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="@string/app_name" android:hint="@string/search_hint" android:searchSuggestAuthority="com.example.google.places.search_suggestion_provider"> </searchable> |
AndroidManifest.xml、searchable.xml 和你的内容提供者中的权限应该相同。
为包含 SearchView (/res/menu/options_menu.xml) 的 ActionBar 创建一个选项菜单。
1
2 3 4 5 6 7 |
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_search" android:title="@string/menu_search" android:icon="@drawable/ic_menu_search" android:showAsAction="collapseActionView|ifRoom" android:actionViewClass="android.widget.SearchView" /> </menu> |
使用与您的可搜索配置相关联的 SearchView 配置您的 Activity/
1
2 3 4 5 6 7 8 9 10 11 12 13 14 |
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the options menu from XML MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.options_menu, menu); SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); return true; |
几个关键文档是:
添加自定义建议:
http://developer.android.com/guide/topics/search/adding-custom-suggestions.html
创建内容提供者:
http://developer.android.com/guide/topics/providers/content-provider-creating.html
使用搜索小部件:
http://developer.android.com/guide/topics/search/search-dialog.html#UsingSearchWidget
带有谷歌搜索 API 的 AutoCompleteTextView
你的xml
1
2 3 4 5 6 7 8 9 10 11 12 |
<AutoCompleteTextView
android:id="@+id/main_omnibox_input" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:background="@null" android:hint="Search" android:focusable="true" android:focusableInTouchMode="true" android:selectAllOnFocus="true" android:singleLine="true" android:textSize="@dimen/_14sdp" /> |
你的java代码
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
this.inputBox = (AutoCompleteTextView) findViewById(R.id.main_omnibox_input); inputBox.setAdapter(new SearchAutocompleteAdapter(SearchActivity.this, new SearchAutocompleteAdapter.OnSearchCommitListener() { @Override public void onSearchCommit(String text) { inputBox.setText(text); inputBox.setSelection(text.length()); } })); this.inputBox.setOnItemClickListener(new OnItemClickListener() { |
搜索自动完成适配器
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
public class SearchAutocompleteAdapter extends BaseAdapter implements Filterable {
interface OnSearchCommitListener { private final Context mContext; SearchAutocompleteAdapter(Context context, OnSearchCommitListener commitListener) { @Override @Override @Override @SuppressLint("ClickableViewAccessibility") textview.setOnTouchListener(new View.OnTouchListener() { @Override @Override private List<String> getCompletions(String text) { JSONArray jsonArray; |
}
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/269034.html