【发布时间】:2010-12-30 09:10:59
【问题描述】:
我有一个带有自动完成框的活动。每当文本更改时,我想调用 Web 服务并使用返回的新 String[] 填充 ArrayAdapter。这部分一切都很好,除了当 String[] 学校中有所有新值时 UI 中的列表没有被刷新。我的 onCreate 中填充的原始列表始终保留。
我在某处读到需要在运行 UI 的同一线程上更新它,因此我尝试了下面代码中列出的 Runnable。但是,除了更新我的班级变量学校之外,notifyOnDataSetChange()
我哪里出错了?
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
schools = SchoolProxy.search("Co");
autoCompleteAdapter = new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line, schools);
autoComplete = (AutoCompleteTextView) findViewById(R.id.edit);
autoComplete.addTextChangedListener(textChecker);
autoComplete.setAdapter(autoCompleteAdapter);
}
...
....
...
final TextWatcher textChecker = 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)
{
schools = SchoolProxy.search(s.toString());
runOnUiThread(updateAdapter);
}
};
private Runnable updateAdapter = new Runnable() {
public void run() {
autoCompleteAdapter.notifyDataSetChanged();
}
};
作为一个不太重要的旁注,如果学校中的每个项目都有一个名称\n city,state。是否可以在自动下拉框中将城市和州放在第二行?仅用于格式化目的。如果可以的话会看起来更干净。
谢谢!
【问题讨论】:
标签: android autocomplete