java Android AutoCompleteTextView刷新
我正在使用Geocoder从字符串中获取匹配的地址
我正在尝试向AutoCompleteTextView显示返回的地址
当ILog.i("Result:"," "+list_of_addresses);
时,我可以正确地看到这些值
因为我们讨论的是动态列表加载,所以我调用adapter.NotifyDataSetChanged();
我正在使用addTextChangedListener来侦听用户输入的文本中的更改
这就是代码的样子
public class Map extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
final Geocoder gc = new Geocoder(getApplicationContext(), Locale.getDefault());
final ArrayList<String> address_name = new ArrayList<String>();
final AutoCompleteTextView search = (AutoCompleteTextView) findViewById(R.id.search);
search.setThreshold(1);
final ArrayAdapter<String> adapter =new ArrayAdapter<String>(this,
安卓.R.layout.simple_dropdown_item_1line,address_name);
search.setAdapter(adapter); //moved this line out of the try block
search.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
List<Address> list = null;
Address address = null;
try {
list = gc.getFromLocationName(s.toString(), 10);
Log.i("List:", ""+list); //CAN see this log
} catch (IOException e) {
e.printStackTrace();
}
for(int i=0; i<list.size(); i++){
address = list.get(i);
address_name.add(address.getFeatureName().toString());
Log.i("Address: ", address.getFeatureName().toString()); //CANto see this Log
}
if(!list.isEmpty()){
list.clear();
}
adapter.notifyDataSetChanged();
search.showDropDown();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
});
}
}
在for循环中,我无法看到log.i("Addresses:",address.getFeatureName.toString());
但是,当我将for循环条件更改为i<=list.size()
时,我确实获得了日志输出,但是应用程序强制关闭时出现了这个错误java.lang.IndexOutOfBoundsException: Invalid index 10, size is 10
也许这就是为什么我看不到地址列表
EDIT:我将for循环条件更改为i<list.size()
,并在通知数据集更改后添加了search.showDropDown()
任何帮助都将不胜感激!谢谢
# 1 楼答案
该问题与适配器方法的错误或空重写有关:
您必须实现上述方法。 它会起作用的
# 2 楼答案
你似乎在让它工作上有问题。我将粘贴对我非常有效的内容,只粘贴自动完成的相关部分
onCreate
布局xml
自动完成类,别忘了替换API密钥