我有关于它的自动完成框的活动。每当文字的变化,我想调用Web服务并填充一个ArrayAdapter新的String []返回。这部分所有的伟大工程,除了列表中的用户界面时,有在的String []学校所有的新值没有被刷新。居住在我的onCreate原来的名单始终保持。
I have an Activity with an AutoComplete box on it. Whenever the text changes, I want to call a web service and populate the ArrayAdapter with the new String[] returned. This part all works great, except the list in the UI isn't being refreshed when there are all new values in String[] schools. The original list populated in my onCreate always remains.
我读的地方,我需要在用户界面上运行同一个线程更新它,所以我想在我下面的code了Runnable上市。但是,以及刚刚更新我的类变量,学校不靠notifyOnDataSetChange工作()
I read somewhere I needed to update it on the same thread the UI is running on, so I tried the Runnable listed in my code below. But that, as well as just updating my class variable schools does not work alongside 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市,州。是否有可能有一个城市和放大器;国家对汽车内的第二条生产线下拉框?只是,它们的目的。如果我能看起来更干净。
As a less important side note, if each item in schools has a name \n city,state. Is it possible to have the city & state on a second line within the auto drop down box? Just for formatting purposes. Would look cleaner if I could.
感谢您!
您是否尝试过的 setNotifyOnChange(真)
?它应该正确地做你手动做什么,只要您使用更改列表方法(添加(T),插入(T,INT),删除(T),明确的())
。也许你有过在 ArrayAdapter
这些方法来修改数组?
Have you tried the setNotifyOnChange(true)
? It is supposed to properly do what you are doing manually whenever you use methods that change the list (add(T), insert(T, int), remove(T), clear())
. Perhaps you have to modify the array through these methods on the ArrayAdapter
?
我不知道,如果 ArrayAdapter
实际上是抱着参照学校或刚复制的内容,同时构建 ArrayAdapter
。也许你可以看看在AOSP code,看看他们正在做的这个构造的东西。
I'm not sure if the ArrayAdapter
is actually holding the reference to schools or just copied the contents while constructing the ArrayAdapter
. Maybe you can take a look at the AOSP code to see what they're doing in that constructor.