带有 ArrayAdapter 和 TextWatcher 的动态 AutocompleteTextView
我正在尝试使用 ArrayAdapter 动态更新 AutocompleteTextView 的列表。为了更新此视图,我使用 TextWatcher 来监视 AutocompleteTextView 中可能发生的任何更改。
问题是该列表根本没有更新,我不明白为什么。我一直在互联网上寻找类似的方法,并且找到了几种不同的方法,但我仍然无法理解为什么这个应该是最简单的方法不起作用。任何解释将不胜感激。
目标 AVD:Google API 级别 10,Android 2.3.3
以下是简化的代码:
public class AutocompleteActivity extends Activity implements TextWatcher {
ArrayAdapter<String> adapter = null;
AutoCompleteTextView acTextView = null;
ArrayList<String> addresses = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.autocompleteview);
acTextView = (AutoCompleteTextView) findViewById(R.id.autocomplete_address);
adapter = new ArrayAdapter<String>(this, R.layout.listitem, addresses);
adapter.setNotifyOnChange(true);
acTextView.addTextChangedListener(this);
acTextView.setAdapter(adapter);
}
@Override
public void onTextChanged(CharSequence text, int start, int before, int after) {
try {
adapter.add("test");
}
catch (Exception e) {
e.printStackTrace();
}
}
I'm trying to update the list of an AutocompleteTextView dynamically using and ArrayAdapter. In order to update this View I use a TextWatcher to monitor any changes that may occur in the AutocompleteTextView.
The problem is that the list isn't updating at all and I can't understand why. I've been looking for something like that on internet and I've found couple of different approaches but still I can't understand why this one, that should be the simplest one, isn't working. Any explaination would be much appreciated.
Target AVD: Google APIs level 10, Android 2.3.3
Here is the simplified code:
public class AutocompleteActivity extends Activity implements TextWatcher {
ArrayAdapter<String> adapter = null;
AutoCompleteTextView acTextView = null;
ArrayList<String> addresses = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.autocompleteview);
acTextView = (AutoCompleteTextView) findViewById(R.id.autocomplete_address);
adapter = new ArrayAdapter<String>(this, R.layout.listitem, addresses);
adapter.setNotifyOnChange(true);
acTextView.addTextChangedListener(this);
acTextView.setAdapter(adapter);
}
@Override
public void onTextChanged(CharSequence text, int start, int before, int after) {
try {
adapter.add("test");
}
catch (Exception e) {
e.printStackTrace();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的源代码中,TextWatcher 被提供给 AutoCompleteTextView,这是真正的问题。
如果你查看AutoCompleteTextView源代码,你会发现AutoCompleteTextView有它的
自己的TextWatcher,命名为“MyWatcher”。因此,AutoCompleteTextView 无法响应您的键入操作。
In your source code, TextWatcher is supplied to the AutoCompleteTextView, this is the real problem.
If you look at AutoCompleteTextView source code, you will find AutoCompleteTextView has its
own TextWatcher, named "MyWatcher". Becauseof this, AutoCompleteTextView can not repond to your typing action.