我有一个 AutoCompleteTextView 这给用户的自动完成搜索结果从谷歌的地方API 。有一次,我做我发现的搜索查看,以及它如何可以被放置在动作条。我检查了谷歌提供的搜索查看例子,并把它添加到我的应用程序为起点(它列出了安装的应用程序),但我不知道如何从这里出发。我希望有相同的功能,但AutoCompleteTextView使用和SearchView代替。 任何建议? 全班下面提供。
进口java.io.BufferedReader中;
进口java.io.IOException异常;
进口java.io.InputStreamReader中;
进口的java.net.URL;
进口java.net.URLConnection中;
进口java.net.URLEn codeR;
进口的java.util.ArrayList;
进口的java.util.List;
进口org.json.JSONArray;
进口org.json.JSONException;
进口org.json.JSONObject;
进口android.app.Activity;
进口android.app.SearchManager;
进口android.app.SearchableInfo;
进口android.content.Context;
进口android.content.Intent;
进口android.content.Shared preferences;
进口android.content.Shared preferences.OnShared preferenceChangeListener;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.os.Handler;
。进口的Android preference preferenceManager。
进口android.text.Editable;
进口android.text.TextWatcher;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.Menu;
进口android.view.MenuInflater;
进口android.view.MenuItem;
进口android.view.View;
进口android.view.Window;
进口android.view.animation.Animation;
进口android.view.animation.AnimationUtils;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.ArrayAdapter;
进口android.widget.AutoCompleteTextView;
进口android.widget.ImageView;
进口android.widget.ListView;
进口android.widget.SearchView;
进口android.widget.TextView;
进口android.widget.Toast;
/ **
*
* @作者
*
* /
公共类PlacesListSearchActivity扩展活动实现SearchView.OnQueryTextListener {
私有静态最后字符串变量=PlacesListActivity;
私人ResultReceiver mReceiver;
私人OnShared preferenceChangeListener共享preferencesListener;
私人共享preferences共享preferences;
/ **第一次创建活动时调用。 * /
公共ArrayAdapter<字符串>适配器;
公共AutoCompleteTextView TextView的;
私人搜索查看mSearchView;
私人TextView的mStatusView;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
。getWindow()requestFeature(Window.FEATURE_ACTION_BAR);
的setContentView(R.layout.places_search);
mStatusView =(TextView中)findViewById(R.id.status_text);
最后ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(这一点,R.layout.item_list);
TextView的=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
adapter.setNotifyOnChange(真正的);
textView.setHint(式的商店名称);
textView.setAdapter(适配器);
textView.addTextChangedListener(新TextWatcher(){
公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
如果(计数%3 == 1){
adapter.clear();
GetPlaces任务=新GetPlaces();
//现在在TextView中传递参数给任务
task.execute(textView.getText()的toString());
}
}
公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
之后INT){
// TODO自动生成方法存根
}
公共无效afterTextChanged(编辑S){
}
});
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
super.onCreateOptionsMenu(菜单);
MenuInflater充气= getMenuInflater();
inflater.inflate(R.menu.searchview_in_menu,菜单);
菜单项searchItem = menu.findItem(R.id.action_search);
mSearchView =(搜索查看)searchItem.getActionView();
setupSearchView(searchItem);
返回true;
}
私人无效setupSearchView(菜单项searchItem){
如果(isAlwaysExpanded()){
mSearchView.setIconifiedByDefault(假);
} 其他 {
searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
}
SearchManager searchManager =(SearchManager)getSystemService(Context.SEARCH_SERVICE);
如果(searchManager!= NULL){
名单< SearchableInfo>可搜寻= searchManager.getSearchablesInGlobalSearch();
//尝试使用应用程序全局搜索提供商
SearchableInfo信息= searchManager.getSearchableInfo(getComponentName());
对于(SearchableInfo INF:可搜寻){
如果(inf.getSuggestAuthority()!=空
&功放;&安培; inf.getSuggestAuthority()。startsWith(应用程序)){
信息= INF;
}
}
mSearchView.setSearchableInfo(信息);
}
mSearchView.setOnQueryTextListener(本);
}
公共布尔onQueryTextChange(字符串newText){
mStatusView.setText(查询=+ newText);
返回false;
}
公共布尔onQueryTextSubmit(查询字符串){
mStatusView.setText(查询=+查询+:提交);
返回false;
}
公共布尔的OnClose(){
mStatusView.setText(封闭!);
返回false;
}
保护的布尔isAlwaysExpanded(){
返回false;
}
类GetPlaces扩展的AsyncTask<字符串,无效的ArrayList<字符串>> {
@覆盖
//三个点是用java字符串数组
受保护的ArrayList<字符串> doInBackground(字符串参数... args)
{
Log.d(PlacesListActivity,doInBackground);
ArrayList的<字符串> predictionsArr =新的ArrayList<字符串>();
尝试
{
URL googlePlaces =新的URL(
https://maps.googleapis.com/maps/api/place/autocomplete/json?input=+
URLEn coder.en code(参数[0],UTF-8)+
//&放大器;类型=地理code和;语言= EN&放大器;传感器=真放大器;关键=+搜索GEO codeS
&放大器;类型=建立和放大器;语言= EN&放大器;传感器=真放大器;关键=+
。getResources()的getString(R.string.googleAPIKey));
Log.d(PlacesListActivity,googlePlaces.toString());
的URLConnection TC = googlePlaces.openConnection();
的BufferedReader在=新的BufferedReader(新的InputStreamReader(
tc.getInputStream()));
串线;
StringBuffer的SB =新的StringBuffer();
//拿谷歌的清晰JSON并把它变成一个大的字符串。
而((行= in.readLine())!= NULL){
sb.append(线);
}
//把该字符串转换成JSON对象
JSONObject的predictions =新的JSONObject(sb.toString());
//现在得到的JSON数组就是这样的对象内
JSONArray JA =新JSONArray(predictions.getString(predictions));
的for(int i = 0; I< ja.length();我++){
的JSONObject祚=(的JSONObject)ja.get(我);
//每个条目添加到我们的数组
predictionsArr.add(jo.getString(说明));
}
}赶上(IOException异常E)
{
Log.e(PlacesListActivity,GetPlaces:doInBackground,E);
}赶上(JSONException E)
{
Log.e(PlacesListActivity,GetPlaces:doInBackground,E);
}
返回predictionsArr;
}
@覆盖
保护无效onPostExecute(ArrayList中<字符串>的结果){
Log.d(PlacesListActivity,onPostExecute:+ result.size());
//更新适配器
适配器=新的ArrayAdapter<字符串>(getBaseContext(),R.layout.item_list);
adapter.setNotifyOnChange(真正的);
//附加适配器的TextView
textView.setAdapter(适配器);
对于(字符串字符串:结果){
Log.d(PlacesListActivity,onPostExecute:结果=+字符串);
adapter.add(串);
adapter.notifyDataSetChanged();
}
Log.d(PlacesListActivity,onPostExecute:autoCompleteAdapter+ adapter.getCount());
}
}
}
更新code。通过萨克斯曼建议后,我可以看到,在提供的查询方法不会被调用:
我的清单文件看起来是这样的:
< XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.rathinavelu.rea
安卓版code =1
机器人:VERSIONNAME =1.0>
<用途-SDK
安卓的minSdkVersion =7
机器人:targetSdkVersion =15/>
<使用-权限的Android:名称=android.permission.INTERNET对/>
<使用-权限的Android:名称=android.permission.ACCESS_FINE_LOCATION/>
<使用-权限的Android:名称=android.permission.WRITE_EXTERNAL_STORAGE/>
<使用-权限的Android:名称=android.permission.ACCESS_NETWORK_STATE/>
<应用
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME>
<活动
机器人:名称=。MainActivity
机器人:标签=@字符串/ APP_NAME
机器人:screenOrientation =画像>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<活动机器人:名称=。PlacesListActivity>
< /活性GT;
<活动机器人:名称=。PlacesListSearchActivity>
<作用机器人:名称=android.intent.action.SEARCH/>
&所述;元数据
机器人:名称=android.app.searchable
机器人:资源=@ XML /搜索/>
< /活性GT;
<活动机器人:名称=。TestMapActivity>
< /活性GT;
<活动机器人:名称=设置preferencesActivity>
< /活性GT;
<活动机器人:名称=com.rathinavelu.util.ConnectionChecker>
< /活性GT;
<使用库机器人:名称=com.google.android.maps/>
<服务
机器人:名称=。places.PlacesRESTService
机器人:启用=真
机器人:出口=假>
<意向滤光器>
<作用机器人:名称=android.intent.action.ACTION_SYNC/>
&所述; /意图滤光器>
< /服务>
<供应商
机器人:名称=。places.PlacesSuggestionProvider
机器人:当局=com.rathinavelu.rea.places.search_suggestion_provider
机器人:可同步=FALSE/>
< /用途>
< /舱单>
我用同样的权力清单,内容提供商和清单文件。我看到菜单中的搜索查看,我还没有修改的查询方法,所以应该只返回一行光标。但查询方法不会被调用。请帮忙。 我刚发现的另一个问题是,和SearchView不显示指定的search_hint!
提供了更多的code * PlacesListSearchActivity.java 的*
公共类PlacesListSearchActivity延伸活动{
私有静态最后字符串变量=PlacesListSearchActivity;
私人ResultReceiver mReceiver;
私人OnShared preferenceChangeListener共享preferencesListener;
私人共享preferences共享preferences;
/ **第一次创建活动时调用。 * /
公共ArrayAdapter<字符串>适配器;
公共AutoCompleteTextView TextView的;
私人搜索查看mSearchView;
私人TextView的mStatusView;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
。getWindow()requestFeature(Window.FEATURE_ACTION_BAR);
的setContentView(R.layout.places_search);
mStatusView =(TextView中)findViewById(R.id.status_text);
最后ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(这一点,R.layout.item_list);
TextView的=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
adapter.setNotifyOnChange(真正的);
textView.setHint(式的商店名称);
textView.setAdapter(适配器);
textView.addTextChangedListener(新TextWatcher(){
公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
如果(计数%3 == 1){
adapter.clear();
GetPlaces任务=新GetPlaces();
//现在在TextView中传递参数给任务
task.execute(textView.getText()的toString());
}
}
公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
之后INT){
// TODO自动生成方法存根
}
公共无效afterTextChanged(编辑S){
}
});
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
// super.onCreateOptionsMenu(菜单);
//充气从XML选项菜单
MenuInflater充气= getMenuInflater();
inflater.inflate(R.menu.places_list_search_options_menu,菜单);
SearchManager searchManager =(SearchManager)getSystemService(Context.SEARCH_SERVICE);
搜索查看搜索查看=(搜索查看)menu.findItem(R.id.menu_search).getActionView();
//告诉你的应用程序的搜索查看使用此活动的可搜索的配置
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(假); //不要图标化的工具;默认情况下,展开
// setupSearchView(searchItem);
返回true;
}
places_search.xml
< XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:后台=#DDDDDD>
< AutoCompleteTextView机器人:ID =@ + ID / autoCompleteTextView1
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginTop =10dp>
< requestFocus的>< / requestFocus的>
< / AutoCompleteTextView>
<的TextView
机器人:ID =@ + ID / STATUS_TEXT
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:重力=center_horizontal/>
< / RelativeLayout的>
places_list_search_options_menu.xml
< XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目机器人:ID =@ + ID / menu_search
机器人:标题=@字符串/ menu_search
机器人:图标=@机器人:可绘制/ ic_menu_search
机器人:showAsAction =collapseActionView | ifRoom
机器人:actionViewClass =android.widget.SearchView/>
< /菜单>
searchable.xml
< XML版本=1.0编码=UTF-8&GT?;
<可搜索的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:标签=@字符串/ APP_NAME
机器人:提示=@字符串/ search_hint
机器人:searchSuggestAuthority =com.rathinavelu.rea.places.search_suggestion_provider>
< /搜索>
PlacesSuggestionProvider.java
公共类PlacesSuggestionProvider扩展ContentProvider的{
私有静态最后弦乐LOG_TAG =PlacesSuggestionProvider;
公共静态最后弦乐管理局=com.rathinavelu.rea.places.search_suggestion_provider;
公共静态最终乌里CONTENT_URI = Uri.parse(内容://+ AUTHORITY +/搜索);
// UriMatcher不断的搜索建议
私有静态最终诠释SEARCH_SUGGEST = 1;
私有静态最后UriMatcher uriMatcher;
私有静态最后的String [] SEARCH_SUGGEST_COLUMNS = {
BaseColumns._ID,
SearchManager.SUGGEST_COLUMN_TEXT_1,
SearchManager.SUGGEST_COLUMN_TEXT_2,
SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID
};
静态{
uriMatcher =新UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(权威,SearchManager.SUGGEST_URI_PATH_QUERY,SEARCH_SUGGEST);
uriMatcher.addURI(权威,SearchManager.SUGGEST_URI_PATH_QUERY +/ *,SEARCH_SUGGEST);
}
@覆盖
公众诠释删除(URI URI,ARG1字符串,字符串[] ARG2){
抛出新UnsupportedOperationException异常();
}
@覆盖
公共字符串的getType(URI URI){
开关(uriMatcher.match(URI)){
案例SEARCH_SUGGEST:
返回SearchManager.SUGGEST_MIME_TYPE;
默认:
抛出新抛出:IllegalArgumentException(未知的URL+ URI);
}
}
@覆盖
公众开放的插入(URI URI,ContentValues ARG1){
抛出新UnsupportedOperationException异常();
}
@覆盖
公共布尔的onCreate(){
返回true;
}
@覆盖
公共光标查询(URI URI,字符串[]投影,选择字符串,字符串[] selectionArgs两个,
串排序顺序){
Log.d(LOG_TAG,查询=+ URI);
//使用UriMatcher,看看有什么样的查询,我们有
开关(uriMatcher.match(URI)){
案例SEARCH_SUGGEST:
Log.d(LOG_TAG,搜索建议请求。);
MatrixCursor光标=新MatrixCursor(SEARCH_SUGGEST_COLUMNS,1);
cursor.addRow(新的String [] {
1,搜索结果,搜索结果描述,CONTENT_ID
});
返回游标;
默认:
抛出新抛出:IllegalArgumentException(未知乌里:+ URI);
}
}
@覆盖
公众诠释更新(开放的我们的URI,ContentValues ARG1,ARG2字符串,字符串[] ARG3){
抛出新UnsupportedOperationException异常();
}
}
解决方案
要获得商家信息自动填充API导致搜索查看,您首先需要ContentProvider的的API。
进口android.app.SearchManager;
进口android.content.ContentProvider;
进口android.content.ContentValues;
进口android.content.UriMatcher;
进口android.database.Cursor;
进口android.database.MatrixCursor;
进口android.net.Uri;
进口android.provider.BaseColumns;
进口android.util.Log;
公共类PlacesSuggestionProvider扩展ContentProvider的{
私有静态最后弦乐LOG_TAG =ExampleApp中;
公共静态最后弦乐管理局=com.example.google.places.search_suggestion_provider;
公共静态最终乌里CONTENT_URI = Uri.parse(内容://+ AUTHORITY +/搜索);
// UriMatcher不断的搜索建议
私有静态最终诠释SEARCH_SUGGEST = 1;
私有静态最后UriMatcher uriMatcher;
私有静态最后的String [] SEARCH_SUGGEST_COLUMNS = {
BaseColumns._ID,
SearchManager.SUGGEST_COLUMN_TEXT_1,
SearchManager.SUGGEST_COLUMN_TEXT_2,
SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID
};
静态{
uriMatcher =新UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(权威,SearchManager.SUGGEST_URI_PATH_QUERY,SEARCH_SUGGEST);
uriMatcher.addURI(权威,SearchManager.SUGGEST_URI_PATH_QUERY +/ *,SEARCH_SUGGEST);
}
@覆盖
公众诠释删除(URI URI,ARG1字符串,字符串[] ARG2){
抛出新UnsupportedOperationException异常();
}
@覆盖
公共字符串的getType(URI URI){
开关(uriMatcher.match(URI)){
案例SEARCH_SUGGEST:
返回SearchManager.SUGGEST_MIME_TYPE;
默认:
抛出新抛出:IllegalArgumentException(未知的URL+ URI);
}
}
@覆盖
公众开放的插入(URI URI,ContentValues ARG1){
抛出新UnsupportedOperationException异常();
}
@覆盖
公共布尔的onCreate(){
返回true;
}
@覆盖
公共光标查询(URI URI,字符串[]投影,选择字符串,字符串[] selectionArgs两个,
串排序顺序){
Log.d(LOG_TAG,查询=+ URI);
//使用UriMatcher,看看有什么样的查询,我们有
开关(uriMatcher.match(URI)){
案例SEARCH_SUGGEST:
Log.d(LOG_TAG,搜索建议请求。);
MatrixCursor光标=新MatrixCursor(SEARCH_SUGGEST_COLUMNS,1);
cursor.addRow(新的String [] {
1,搜索结果,搜索结果描述,CONTENT_ID
});
返回游标;
默认:
抛出新抛出:IllegalArgumentException(未知乌里:+ URI);
}
}
@覆盖
公众诠释更新(开放的我们的URI,ContentValues ARG1,ARG2字符串,字符串[] ARG3){
抛出新UnsupportedOperationException异常();
}
}
添加您的商家信息自动填充的API客户端code进入的查询内容提供商的方法。您提取用户输入如下:
查询字符串= uri.getLastPathSegment()与toLowerCase()。
添加PlacesSuggestionProvider你AndroidManifest,并确保活动有一个可搜索的配置。
<应用
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME>
<活动机器人:名称=。PlacesSearchViewActivity>
<意向滤光器>
<作用机器人:名称=android.intent.action.SEARCH/>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
&所述;元数据
机器人:名称=android.app.searchable
机器人:资源=@ XML /搜索/>
< /活性GT;
<供应商
机器人:名称=com.example.google.places.PlacesSuggestionProvider
机器人:当局=com.example.google.places.search_suggestion_provider
机器人:可同步=FALSE/>
< /用途>
< /舱单>
,并确保您搜索的配置(RES / XML / searchable.xml)有一个搜索建议的权威。
< XML版本=1.0编码=UTF-8&GT?;
<可搜索的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:标签=@字符串/ APP_NAME
机器人:提示=@字符串/ search_hint
机器人:searchSuggestAuthority =com.example.google.places.search_suggestion_provider>
< /搜索>
该机构应在AndroidManifest.xml中,searchable.xml和内容提供商一样。
创建您的ActionBar一个选项菜单,其中包括一个搜索查看(/res/menu/options_menu.xml)。
<菜单的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目机器人:ID =@ + ID / menu_search
机器人:标题=@字符串/ menu_search
机器人:图标=@可绘制/ ic_menu_search
机器人:showAsAction =collapseActionView | ifRoom
机器人:actionViewClass =android.widget.SearchView/>
< /菜单>
与同您搜索的结构相关联的搜索查看配置您的活动/
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气从XML选项菜单
MenuInflater充气= getMenuInflater();
inflater.inflate(R.menu.options_menu,菜单);
SearchManager searchManager =(SearchManager)getSystemService(Context.SEARCH_SERVICE);
搜索查看搜索查看=(搜索查看)menu.findItem(R.id.menu_search).getActionView();
//告诉你的应用程序的搜索查看使用此活动的可搜索的配置
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(假); //不要图标化的工具;默认情况下,展开
返回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
I have a AutoCompleteTextView which gives user auto-completion search result from Google Places API. Once I was done I discovered SearchView and how it can be placed in the ActionBar. I checked out the SearchView example provided by google and added it to my application as a starting point (it lists the installed applications) but I don´t know how to proceed from here. I want to have the same functionality as AutoCompleteTextView but use the SearchView instead. Any suggestion? The whole class is provided below.
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 org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;
/**
*
* @author
*
*/
public class PlacesListSearchActivity extends Activity implements SearchView.OnQueryTextListener{
private static final String TAG = "PlacesListActivity";
private ResultReceiver mReceiver;
private OnSharedPreferenceChangeListener sharedPreferencesListener;
private SharedPreferences sharedPreferences;
/** Called when the activity is first created. */
public ArrayAdapter<String> adapter;
public AutoCompleteTextView textView;
private SearchView mSearchView;
private TextView mStatusView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.places_search);
mStatusView = (TextView) findViewById(R.id.status_text);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.item_list);
textView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
adapter.setNotifyOnChange(true);
textView.setHint("type store name");
textView.setAdapter(adapter);
textView.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count%3 == 1) {
adapter.clear();
GetPlaces task = new GetPlaces();
//now pass the argument in the textview to the task
task.execute(textView.getText().toString());
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.searchview_in_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) searchItem.getActionView();
setupSearchView(searchItem);
return true;
}
private void setupSearchView(MenuItem searchItem) {
if (isAlwaysExpanded()) {
mSearchView.setIconifiedByDefault(false);
} else {
searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
}
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
if (searchManager != null) {
List<SearchableInfo> searchables = searchManager.getSearchablesInGlobalSearch();
// Try to use the "applications" global search provider
SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
for (SearchableInfo inf : searchables) {
if (inf.getSuggestAuthority() != null
&& inf.getSuggestAuthority().startsWith("applications")) {
info = inf;
}
}
mSearchView.setSearchableInfo(info);
}
mSearchView.setOnQueryTextListener(this);
}
public boolean onQueryTextChange(String newText) {
mStatusView.setText("Query = " + newText);
return false;
}
public boolean onQueryTextSubmit(String query) {
mStatusView.setText("Query = " + query + " : submitted");
return false;
}
public boolean onClose() {
mStatusView.setText("Closed!");
return false;
}
protected boolean isAlwaysExpanded() {
return false;
}
class GetPlaces extends AsyncTask<String, Void, ArrayList<String>> {
@Override
// three dots is java for an array of strings
protected ArrayList<String> doInBackground(String... args)
{
Log.d("PlacesListActivity", "doInBackground");
ArrayList<String> predictionsArr = new ArrayList<String>();
try
{
URL googlePlaces = new URL(
"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=" +
URLEncoder.encode(args[0], "UTF-8") +
// "&types=geocode&language=en&sensor=true&key=" + SEARCHES FOR GEO CODES
"&types=establishment&language=en&sensor=true&key=" +
getResources().getString(R.string.googleAPIKey));
Log.d("PlacesListActivity", googlePlaces.toString());
URLConnection tc = googlePlaces.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
tc.getInputStream()));
String line;
StringBuffer sb = new StringBuffer();
//take Google's legible JSON and turn it into one big string.
while ((line = in.readLine()) != null) {
sb.append(line);
}
//turn that string into a JSON object
JSONObject predictions = new JSONObject(sb.toString());
//now get the JSON array that's inside that object
JSONArray ja = new JSONArray(predictions.getString("predictions"));
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
//add each entry to our array
predictionsArr.add(jo.getString("description"));
}
} catch (IOException e)
{
Log.e("PlacesListActivity", "GetPlaces : doInBackground", e);
} catch (JSONException e)
{
Log.e("PlacesListActivity", "GetPlaces : doInBackground", e);
}
return predictionsArr;
}
@Override
protected void onPostExecute(ArrayList<String> result){
Log.d("PlacesListActivity", "onPostExecute : " + result.size());
//update the adapter
adapter = new ArrayAdapter<String>(getBaseContext(), R.layout.item_list);
adapter.setNotifyOnChange(true);
//attach the adapter to textview
textView.setAdapter(adapter);
for (String string : result) {
Log.d("PlacesListActivity", "onPostExecute : result = " + string);
adapter.add(string);
adapter.notifyDataSetChanged();
}
Log.d("PlacesListActivity", "onPostExecute : autoCompleteAdapter" + adapter.getCount());
}
}
}
After updating the code suggested by saxman, I can see that the query method in the provider is never called:
My manifest file looks like this:
<?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
android:minSdkVersion="7"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".PlacesListActivity" >
</activity>
<activity android:name=".PlacesListSearchActivity" >
<action android:name="android.intent.action.SEARCH" />
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<activity android:name=".TestMapActivity" >
</activity>
<activity android:name=".SettingsPreferencesActivity" >
</activity>
<activity android:name="com.rathinavelu.util.ConnectionChecker" >
</activity>
<uses-library android:name="com.google.android.maps" />
<service
android:name=".places.PlacesRESTService"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.ACTION_SYNC" />
</intent-filter>
</service>
<provider
android:name=".places.PlacesSuggestionProvider"
android:authorities="com.rathinavelu.rea.places.search_suggestion_provider"
android:syncable="false" />
</application>
</manifest>
I use the same authority in the manifest, content provider and manifest file. I see the searchView in the menu and I have not modified the query method so It should just return the one line cursor. but the query method is never called. please help. Another issue I just spotted is that the searchView does not show the specified search_hint!
Providing more code *PlacesListSearchActivity.java*
public class PlacesListSearchActivity extends Activity {
private static final String TAG = "PlacesListSearchActivity";
private ResultReceiver mReceiver;
private OnSharedPreferenceChangeListener sharedPreferencesListener;
private SharedPreferences sharedPreferences;
/** Called when the activity is first created. */
public ArrayAdapter<String> adapter;
public AutoCompleteTextView textView;
private SearchView mSearchView;
private TextView mStatusView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.places_search);
mStatusView = (TextView) findViewById(R.id.status_text);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.item_list);
textView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
adapter.setNotifyOnChange(true);
textView.setHint("type store name");
textView.setAdapter(adapter);
textView.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count%3 == 1) {
adapter.clear();
GetPlaces task = new GetPlaces();
//now pass the argument in the textview to the task
task.execute(textView.getText().toString());
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// super.onCreateOptionsMenu(menu);
// Inflate the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.places_list_search_options_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
// Tells your app's SearchView to use this activity's searchable configuration
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
// setupSearchView(searchItem);
return true;
}
places_search.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#dddddd">
<AutoCompleteTextView android:id="@+id/autoCompleteTextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" >
<requestFocus></requestFocus>
</AutoCompleteTextView>
<TextView
android:id="@+id/status_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"/>
</RelativeLayout>
places_list_search_options_menu.xml
<?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>
searchable.xml
<?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
public class PlacesSuggestionProvider extends ContentProvider {
private static final String LOG_TAG = "PlacesSuggestionProvider";
public static final String AUTHORITY = "com.rathinavelu.rea.places.search_suggestion_provider";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/search");
// UriMatcher constant for search suggestions
private static final int SEARCH_SUGGEST = 1;
private static final UriMatcher uriMatcher;
private static final String[] SEARCH_SUGGEST_COLUMNS = {
BaseColumns._ID,
SearchManager.SUGGEST_COLUMN_TEXT_1,
SearchManager.SUGGEST_COLUMN_TEXT_2,
SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID
};
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH_SUGGEST);
uriMatcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", SEARCH_SUGGEST);
}
@Override
public int delete(Uri uri, String arg1, String[] arg2) {
throw new UnsupportedOperationException();
}
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)) {
case SEARCH_SUGGEST:
return SearchManager.SUGGEST_MIME_TYPE;
default:
throw new IllegalArgumentException("Unknown URL " + uri);
}
}
@Override
public Uri insert(Uri uri, ContentValues arg1) {
throw new UnsupportedOperationException();
}
@Override
public boolean onCreate() {
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sortOrder) {
Log.d(LOG_TAG, "query = " + uri);
// Use the UriMatcher to see what kind of query we have
switch (uriMatcher.match(uri)) {
case SEARCH_SUGGEST:
Log.d(LOG_TAG, "Search suggestions requested.");
MatrixCursor cursor = new MatrixCursor(SEARCH_SUGGEST_COLUMNS, 1);
cursor.addRow(new String[] {
"1", "Search Result", "Search Result Description", "content_id"
});
return cursor;
default:
throw new IllegalArgumentException("Unknown Uri: " + uri);
}
}
@Override
public int update(Uri uri, ContentValues arg1, String arg2, String[] arg3) {
throw new UnsupportedOperationException();
}
}
解决方案
To get Places Autocomplete API results in a SearchView, you'll first need a ContentProvider for the API.
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 {
private static final String LOG_TAG = "ExampleApp";
public static final String AUTHORITY = "com.example.google.places.search_suggestion_provider";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/search");
// UriMatcher constant for search suggestions
private static final int SEARCH_SUGGEST = 1;
private static final UriMatcher uriMatcher;
private static final String[] SEARCH_SUGGEST_COLUMNS = {
BaseColumns._ID,
SearchManager.SUGGEST_COLUMN_TEXT_1,
SearchManager.SUGGEST_COLUMN_TEXT_2,
SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID
};
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH_SUGGEST);
uriMatcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", SEARCH_SUGGEST);
}
@Override
public int delete(Uri uri, String arg1, String[] arg2) {
throw new UnsupportedOperationException();
}
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)) {
case SEARCH_SUGGEST:
return SearchManager.SUGGEST_MIME_TYPE;
default:
throw new IllegalArgumentException("Unknown URL " + uri);
}
}
@Override
public Uri insert(Uri uri, ContentValues arg1) {
throw new UnsupportedOperationException();
}
@Override
public boolean onCreate() {
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sortOrder) {
Log.d(LOG_TAG, "query = " + uri);
// Use the UriMatcher to see what kind of query we have
switch (uriMatcher.match(uri)) {
case SEARCH_SUGGEST:
Log.d(LOG_TAG, "Search suggestions requested.");
MatrixCursor cursor = new MatrixCursor(SEARCH_SUGGEST_COLUMNS, 1);
cursor.addRow(new String[] {
"1", "Search Result", "Search Result Description", "content_id"
});
return cursor;
default:
throw new IllegalArgumentException("Unknown Uri: " + uri);
}
}
@Override
public int update(Uri uri, ContentValues arg1, String arg2, String[] arg3) {
throw new UnsupportedOperationException();
}
}
Then add your Places Autocomplete API client code into the query method on the content provider. You extract the user input as follows:
String query = uri.getLastPathSegment().toLowerCase();
Add the PlacesSuggestionProvider to your AndroidManifest, and make sure your activity has a searchable configuration.
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity android:name=".PlacesSearchViewActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<provider
android:name="com.example.google.places.PlacesSuggestionProvider"
android:authorities="com.example.google.places.search_suggestion_provider"
android:syncable="false" />
</application>
</manifest>
And make sure your searchable configuration (res/xml/searchable.xml) has a search suggest authority.
<?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>
The authority should be the same in AndroidManifest.xml, searchable.xml, and your content provider.
Create a options menu for your ActionBar that includes a SearchView (/res/menu/options_menu.xml).
<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>
Configure your Activity with a SearchView that's associated with your searchable configuration/
@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);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
// Tells your app's SearchView to use this activity's searchable configuration
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
return true;
}
A few key docs are:
Adding Custom Suggestions: http://developer.android.com/guide/topics/search/adding-custom-suggestions.html
Creating a Content Provider: http://developer.android.com/guide/topics/providers/content-provider-creating.html
Using a Search Widget: http://developer.android.com/guide/topics/search/search-dialog.html#UsingSearchWidget