Android 如何获取自动完成文本视图google places api?
我正在开发一个应用程序,我想在使用此链接的自动完成文本中输入相关单词时显示城市名称,但在自动完成的rextview中不显示任何相关名称。在Log Cat中没有错误。请告诉我代码中的任何更改。提前谢谢 我的代码 CustomAutoCompleteTextViewAndroid 如何获取自动完成文本视图google places api?,android,google-maps,Android,Google Maps,我正在开发一个应用程序,我想在使用此链接的自动完成文本中输入相关单词时显示城市名称,但在自动完成的rextview中不显示任何相关名称。在Log Cat中没有错误。请告诉我代码中的任何更改。提前谢谢 我的代码 CustomAutoCompleteTextView public class CustomAutoCompleteTextView extends AutoCompleteTextView { public CustomAutoCompleteTextView(Context
public class CustomAutoCompleteTextView extends AutoCompleteTextView {
public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/** Returns the place description corresponding to the selected item */
@Override
protected CharSequence convertSelectionToString(Object selectedItem) {
/** Each item in the autocompetetextview suggestion list is a hashmap object */
HashMap<String, String> hm = (HashMap<String, String>) selectedItem;
return hm.get("description");
}
}
公共类CustomAutoCompleteTextView扩展了AutoCompleteTextView{
公共CustomAutoCompleteTextView(上下文上下文、属性集属性){
超级(上下文,attrs);
}
/**返回与所选项目对应的位置描述*/
@凌驾
受保护的字符序列转换SelectionToString(对象selectedItem){
/**AutoCompetExtView建议列表中的每个项都是hashmap对象*/
HashMap hm=(HashMap)selectedItem;
返回hm.get(“说明”);
}
}
PlaceJSONParser
/** Receives a JSONObject and returns a list */
public List<HashMap<String,String>> parse(JSONObject jObject){
JSONArray jPlaces = null;
try {
/** Retrieves all the elements in the 'places' array */
jPlaces = jObject.getJSONArray("predictions");
} catch (JSONException e) {
e.printStackTrace();
}
/** Invoking getPlaces with the array of json object
* where each json object represent a place
*/
return getPlaces(jPlaces);
}
private List<HashMap<String, String>> getPlaces(JSONArray jPlaces){
int placesCount = jPlaces.length();
List<HashMap<String, String>> placesList = new ArrayList<HashMap<String,String>>();
HashMap<String, String> place = null;
/** Taking each place, parses and adds to list object */
for(int i=0; i<placesCount;i++){
try {
/** Call getPlace with place JSON object to parse the place */
place = getPlace((JSONObject)jPlaces.get(i));
placesList.add(place);
} catch (JSONException e) {
e.printStackTrace();
}
}
return placesList;
}
/** Parsing the Place JSON object */
private HashMap<String, String> getPlace(JSONObject jPlace){
HashMap<String, String> place = new HashMap<String, String>();
String id="";
String reference="";
String description="";
try {
description = jPlace.getString("description");
id = jPlace.getString("id");
reference = jPlace.getString("reference");
place.put("description", description);
place.put("_id",id);
place.put("reference",reference);
} catch (JSONException e) {
e.printStackTrace();
}
return place;
}
}
/**接收JSONObject并返回列表*/
公共列表解析(JSONObject jObject){
JSONArray jPlaces=null;
试试{
/**检索“places”数组中的所有元素*/
jPlaces=jObject.getJSONArray(“预测”);
}捕获(JSONException e){
e、 printStackTrace();
}
/**使用json对象数组调用getPlaces
*其中每个json对象表示一个位置
*/
返回getPlaces(jPlaces);
}
私有列表getPlaces(JSONArray jPlaces){
int placeScont=jPlaces.length();
List placesList=new ArrayList();
HashMap place=null;
/**在每个位置解析并添加到列表对象*/
对于(int i=0;i您的internet可能有问题。请检查您的internet连接。在您的MainActivity.java:文件中,您没有添加“我的API密钥”
在这条线上
String key = "MY API KEY";
在那里添加API密钥并尝试。。。!
加上这样的话:
String key=“key=此处的密钥”
谢谢
String key = "MY API KEY";