【发布时间】:2019-09-14 20:25:21
【问题描述】:
我的场景
我正在构建一个使用 Google 地图的 android 应用程序。
我正在使用以 Json 形式为我提供谷歌位置名称的 API 和 我在显示位置提示的适配器上添加了该名称。 我已经在应用程序中实现了 Place AutoComplete 来给我 当我输入“I”时,自动提示,例如印度、印多尔等。
问题陈述
我的 Json 数组列表返回 NULL,但它应该返回值列表 带有自动建议。
这是我的代码
whitebord.setThreshold(0);
names=new ArrayList<String>();
whitebord.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after)
{
}
public void onTextChanged(CharSequence s, int start,
int before, int count)
{
search_text= whitebord.getText().toString().split(",");
url="https://maps.googleapis.com/maps/api/place/autocomplete/json?
input="+search_text[0]+"&sensor=true&key="+browserKey;
if(search_text.length<=1){
names=new ArrayList<String>();
Log.d("URL",url);
paserdata parse=new paserdata();
parse.execute();
}
}
});
public class paserdata extends AsyncTask<Void, Integer, Void>{
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
JSONParser jParser = new JSONParser();
// getting JSON string from URL
json = jParser.getJSONFromUrl(url.toString());
if(json !=null)
{
try {
// Getting Array of Contacts
contacts = json.getJSONArray(TAG_RESULT);
for(int i = 0; i < contacts.length(); i++){
JSONObject c = contacts.getJSONObject(i);
String description = c.getString("description");
Log.d("description", description);
names.add(description);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
adp = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, names) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setTextColor(Color.BLACK);
return view;
}
};
Toast.makeText(getApplicationContext(), result+"",
Toast.LENGTH_LONG).show();
whitebord.setAdapter(adp);
}
}
【问题讨论】:
标签: android google-maps autocomplete location