5

我正在尝试使用 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();
        }
    }
4

1 回答 1

3

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

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

于 2011-10-13T07:33:31.990 回答