我有一个AutoCompleteTextView,我想搜索位置,例如: 您键入“vig”,AutoCompleteTextView列表显示5个最佳结果: “4560 Vig”,“Juan Pablo Perez ..”,“The Vig 4041 ......”,“Vig” 另一个例子,你输入“vigo”:它说的是正确的地方:“Vigo,Pontevedra”你可以选择它并把它放在AutoCompleteTextView上。
现在,我所拥有的工作几乎不错,但我有一个错误:
我在AsyncTask中搜索位置:
private class SearchAddress extends AsyncTask<String, Void, String[]> {
@Override
protected String[] doInBackground(String... params) {
//adapter.clear();
String[] addressArray = getStreetList(query);
return addressArray;
}
@Override
protected void onPostExecute(String[] addressArray) {
if(addressArray == null)
Toast.makeText(NewRouteActivity.this, "No address obtained from server", Toast.LENGTH_SHORT).show();
else{
adapter.clear();
for(String address: addressArray){
adapter.add(address); <------HERE IS THE 2ND ERROR
Log.d("ASYNC", address);
}
}
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {}
}
这是我的AutoCompleteTextView代码:
String[] array = {};
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, array);//simple_dropdown_item_1line
autoFrom.setAdapter(adapter);
autoTo.setAdapter(adapter);
asyncSearch = new SearchAddress();
autoFrom.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (((AutoCompleteTextView) autoFrom).isPerformingCompletion()) {return;}
if (s.length() < 2) {
return;
}else{
query = s.toString();
if (asyncSearch.getStatus().equals(AsyncTask.Status.FINISHED)){
asyncSearch = new SearchAddress();
asyncSearch.execute(query);
Log.d("ASYNC", "FINISH GOOD");
Log.d("ASYNC", query);
}else{
Log.d("ASYNC", "CANCEL");
asyncSearch.cancel(false);
asyncSearch = new SearchAddress();
asyncSearch.execute(query);
}
return;
}
}
});
我希望用这个就足够了,getStreetList()效果很好,效果很好。 如果你需要别的东西,请问。
提前致谢!!!
答案 0 :(得分:1)
我不知道这是否会对你有所帮助,但我遇到了TextWatcher和AutoCompleteTextView的问题。您应该在适配器上使用过滤器而不是TextWatcher。
谷歌地方+ AutoCompleteTextView的良好实施:谷歌:
https://developers.google.com/places/training/autocomplete-android