带有 ArrayAdapter 和 TextWatcher 的动态 AutocompleteTextView

发布于 2024-12-05 04:59:44 字数 1253 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终弃我 2024-12-12 04:59:44

在您的源代码中,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文