gpt4 book ai didi

android - 带有 ArrayAdapter 和 TextWatcher 的动态 AutocompleteTextView

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:58:54 25 4
gpt4 key购买 nike

我正在尝试使用 ArrayAdapter 动态更新 AutocompleteTextView 的列表。为了更新此 View ,我使用 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();
}
}

最佳答案

在您的源代码中,TextWatcher 被提供给 AutoCompleteTextView,这才是真正的问题。

如果你看一下 AutoCompleteTextView 的源代码,你会发现 AutoCompleteTextView 有它的自己的TextWatcher,命名为“MyWatcher”。因此,AutoCompleteTextView 无法响应您的键入操作。

关于android - 带有 ArrayAdapter 和 TextWatcher 的动态 AutocompleteTextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7461149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com