问题描述
在我的应用我有延伸的活动 MapActivity
。在那里我把一个 AutoCompleteTextView
和一个名为搜索所以,我写在 AutoCompleteTextView
键,preSS按钮搜索按钮它关系到谷歌地图的位置。 AutoCompleteTextView
是项不小,我在的strings.xml 不在话下。
但我想它应该担任谷歌搜索引擎,像谷歌搜索框,无论我们开始写它自动完成每一个字出现。
事情是,它从谷歌服务器获取数据。是不是?
如果是,那我怎么才能将数据绑定到我的 AutoCompleteTextView 从谷歌服务器,以便它可以作为谷歌搜索框。
我使用Android的API V2.2。
In my application I have an Activity which extends MapActivity
. and there i put an AutoCompleteTextView
and a button called "Search" so what i write within AutoCompleteTextView
AND press Search button it goes to that location in Google map. AutoCompleteTextView
is for small no of items which i mention in strings.xml.But I want it should be worked as google search engine, like in google search box whatever we start to write it auto completes every word there.Thing is that it takes data from google server. Is not it?If it is, then how can i bind data to my AutoCompleteTextView from Google server so that it works as Google search box.I am using android API v2.2.
推荐答案
您必须使用谷歌API的地方,你需要首先生成一个地方API密钥,检查此页:
You have to use Google Places API ,you need to generate a place API key first ,check this page :
在我的情况我已经使用这个code:
In my case i have used this code :
final ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,R.layout.list_item);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
adapter.setNotifyOnChange(true);
textView.setAdapter(adapter);
textView.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) { if (count%3 == 1) { adapter.clear(); try {
URL googlePlaces = new URL(
// URLEncoder.encode(url,"UTF-8");
"https://maps.googleapis.com/maps/api/place/autocomplete/json?input="+ URLEncoder.encode(s.toString(), "UTF-8")
+"&types=geocode&language=fr&sensor=true&key=<getyourAPIkey>");
URLConnection tc = googlePlaces.openConnection();
Log.d("GottaGo", URLEncoder.encode(s.toString()));
BufferedReader in = new BufferedReader(new InputStreamReader(
tc.getInputStream()));
String line;
StringBuffer sb = new StringBuffer();
while ((line = in.readLine()) != null) {
sb.append(line);
}
JSONObject predictions = new JSONObject(sb.toString());
JSONArray ja = new JSONArray(predictions.getString("predictions"));
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
adapter.add(jo.getString("description"));
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
} });
这篇关于android怎么样使AutoCompleteTextView工作作为谷歌搜索框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!